About the size mismatch problem and the array value change problem when the strcpy () function is copying a string array

Source: Internet
Author: User

Tag: print BSP Match color img display Add div address

The source of the problem is a test code that you write yourself:

#include <iostream>using namespacestd;intMain () {Chara[1]; a[0] ='a'; a[1] ='b'; Charb[1]; b[0] ='C'; b[1] ='D';    strcpy (A, b); cout<<"after copy: a[0] ="<<a[0]<<"a[1] ="<<a[1]<<Endl; cout<<"after copy: b[0] ="<<b[0]<<"b[1] ="<<b[1]<<Endl; return 0;}

After that, the value of the two string output is not imaginary:

"After copy: a[0] = c A[1] = d

After copy: b[0] = c B[1] = d "

But:

When I first saw the face, why did I use strcpy () after the copy, the value of the source character array is not correct?

So I wrote some test output code, and display the two-character array header in the memory location of the output code to find out where the problem lies:

#include <iostream>using namespacestd;intMain () {Chara[1]; a[0] ='a'; a[1] ='b'; cout<<"A[0] ="<<a[0]<<"a[1] ="<<a[1]<<Endl; Charb[1]; b[0] ='C'; b[1] ='D'; cout<<"declaration Char b[1] After: a[0] ="<<a[0]<<"a[1] ="<<a[1]<<Endl; cout<<"B[0] ="<<b[0]<<"b[1] ="<<b[1]<<Endl;    strcpy (A, b); printf ("%x\n", a); printf ("%x\n", B); cout<<"after copy: a[0] ="<<a[0]<<"a[1] ="<<a[1]<<Endl; cout<<"after copy: b[0] ="<<b[0]<<"b[1] ="<<b[1]<<Endl; return 0;}

The following output is then obtained:

As you can see, the value in the character array A has been changed since the end of the statement Char B[1] this little hooker!

What is this for?

As you can see, the addresses of thetwo character arrays, A and B, differ by only 1, which is where the problem lies!

At the end of the declaration of Char A[1], this is the case in memory:

53151adb ' a '//here is the start address of the character array a

53151ADC ' B '

53151add ' + '

When the Char b[1] is declared, the memory becomes:

53151ada ' C '//Here is the start address of the character array B

53151adb ' d '//Here is the start address of the character array A, the original ' A ' is covered by the ' d ' in the character array B!

53151ADC ' + '//original ' B ' is covered by the end character in array b

53151add ' + '

This explains why A[1] became a ' d ' problem when the Char b[1] was declared.

So what happens when we use strcpy (A, b)?

This function copies the values in B to a in a, so the memory address grows after the copy is done:

53151ada ' C '//Here is the start address of the character array B

53151adb ' C '//Here is the start address of the character array A, just ' d ' is covered by the ' C ' in the character array B!

53151ADC ' d '//just ' s ' is covered by ' d ' in the character array B!

53151add ' + '

At this time, the output of the character array A and B when the beginning of the show we saw a lump of &%&%¥%@ ... Well.

Welcome everyone in the comment area message exchange ~

About the size mismatch problem and the array value change problem when the strcpy () function is copying a string array

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.