Analysis on the Problem of strcat () connecting two strings

Source: Internet
Author: User

 I. Problems Found

There are three small programs:

(1) Use two pointer variables to point to a string

#include <iostream>using namespace std;#include <stdio.h>#include <string.h> int main(){        char *p = "welcome ";        char *q = "to the house";        strcat(p, q);        cout<<p<<endl;        return 0;}

Debugging and running will be interrupted. The interrupt location is in the main_loop of the strcat. ASM file:

In addition, the following dialog box is displayed:

(2) The first character array with unspecified size is used to store the string, and the second uses pointer variables to point to the string.

#include <iostream>using namespace std;#include <stdio.h>#include <string.h> int main(){        char p[] = "welcome ";        char *q = "to the house";        strcat(p, q);        cout<<p<<endl;        return 0;}

When debugging and running, there will be interruptions. the interruption is at the end of the main function:

In addition, the following dialog box is displayed:

(3) The first character array with the specified size stores the string, and the second uses the pointer variable to point to the string

#include <iostream>using namespace std;#include <stdio.h>#include <string.h> int main(){        char p[50] = "welcome ";        char *q = "to the house";        strcat(p, q);        cout<<p<<endl;        system("PAUSE");        return 0;}

Debug and run without errors. The running result is as follows:

Ii. Problem Analysis

Check the three applets carefully and you will find that they are all used to connect the string "welcome" and "to the house, the difference between them lies in the storage method of the string "welcome. Debugging and running of the first and second programs are interrupted, while the third program runs normally. Why? I finally figured it out.

First, you must thoroughly understand the strcat () function.

Its prototype is char * strcat (char * a, const char * B );

Remove the end character "\ 0" of string a, connect string B to the end of string a, and store it in.

Because strcat () connects A and B and stores them in a, a must have enough storage space; otherwise, it will overflow.

In the first program, the pointer Variable P is used to store the first string. During running, the pointer Variable P points to the memory allocated in the heap and only allocates enough memory for the content it points. After Q is connected to P, naturally P has no other space for Q, so there is a memory write conflict and the interruption occurs.

In the second program, the first string is stored using an array of unspecified sizes p. During running, array P is stored in the stack. Because it does not specify the size, the system specifies the size as the length of the initialization string by default during initialization. After Q is connected to P, P has no extra space for Q, so an error occurs in the figure above.

In the third program, 50 is pre-allocated for Array P, and there is enough extra space to hold the Q, so there is no error in running.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.