-1

I create multi dimensioanl array and write it to console

char a[5][10];

strcpy(a[0], "111111");
strcpy(a[1], "211112");
strcpy(a[2], "311113");
strcpy(a[3], "411114");
strcpy(a[4], "511115");


printf("size : %d \n", sizeof(a));

int i;
for(i = 0; i < 5; i++)
{
    printf("%d : %s \n",i,a[i]);
}

result is

size : 50 
0 : 111111 
1 : 211112 
2 : 311113 
3 : 411114 
4 : 511115 

then I copy the array to another array, and write both of them to console

char a[5][10];
char b[][10]={"0"};

strcpy(a[0], "111111");
strcpy(a[1], "211112");
strcpy(a[2], "311113");
strcpy(a[3], "411114");
strcpy(a[4], "511115");

memcpy(&b,&a,sizeof(a));

printf("sizeof(a) : %d \n", sizeof(a));
int i;
for(i = 0; i < 5; i++)
{
    printf("%d : %s \n",i,a[i]);
}

printf("sizeof(b) : %d \n", sizeof(b));
for(i = 0; i < 5; i++)
{
    printf("%d : %s \n",i,b[i]);
}

result is:

sizeof(a) : 50 
0 :  
1 :  
2 :  
3 :  
4 : 511115 
sizeof(b) : 10 
0 : 111111 
1 : 211112 
2 : 311113 
3 : 411114 
4 : 511115 

what happened variables in a array ? why contents of a array are empty ? I use Ubuntu 14.04 and gcc version is (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

utarid
  • 1,642
  • 4
  • 24
  • 38

2 Answers2

4

The size of b is too small to store all contents of a, so you accessed out of the allocated memory, which is very dangerous.

Do allocate enough memory.

char a[5][10];
char b[5][10]={{"0"}}; /* change this line */

strcpy(a[0], "111111");
strcpy(a[1], "211112");
strcpy(a[2], "311113");
strcpy(a[3], "411114");
strcpy(a[4], "511115");

memcpy(&b,&a,sizeof(a));

printf("sizeof(a) : %d \n", (int)sizeof(a)); /* change this line to pass data having correct type */
int i;
for(i = 0; i < 5; i++)
{
    printf("%d : %s \n",i,a[i]);
}

printf("sizeof(b) : %d \n", (int)sizeof(b)); /* change this line to pass data having correct value */
for(i = 0; i < 5; i++)
{
    printf("%d : %s \n",i,b[i]);
}

This code may show why the contents in a seems deleted: 0x00 is written to a[i][0] and printf() interpreted them as end of string.

#include <stdio.h>

int main(void) {
    char a[5][10];
    char b[][10]={"0"};

    strcpy(a[0], "111111");
    strcpy(a[1], "211112");
    strcpy(a[2], "311113");
    strcpy(a[3], "411114");
    strcpy(a[4], "511115");

    memcpy(&b,&a,sizeof(a));

    int i, j;
    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 10; j++) printf("%02X ", (unsigned int)(unsigned char)a[i][j]);
        putchar('\n');
    }

    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

The memcpy overwrote the contents of the array a. Why did it do that?

The array b was allocated only 10 bytes, because char b[][10] means "allocate space for an array of 10-char strings, the number of strings being specified by the initializer." In your case, the initializer is "0", ie, one string. So it allocated space for one 10-char string.

Since the whole thing is on the stack, the layout is reversed. Thus, the location of b comes first, and then a.

So, when you write into b, it starts correctly, writes the first string. When you write the second string, it overflows into the array a's space and kills it. 4 times. The last string survives.

user3392439
  • 973
  • 8
  • 6
  • 2
    "Since the whole thing is on the stack, the layout is reversed." No, it need not be. In this case, `a` just happened to be after `b`. – MikeCAT Feb 24 '16 at 12:39
  • 2
    The C language does not require uising a stack. At this level, there is no use in going into details which can vary with the next compiler version, some randomised heuristics, added code, etc. For a typical implementation, the stack layout can heavily vary, e.g. when re-using allocated space on the stack. – too honest for this site Feb 24 '16 at 12:40
  • Yes, agree on both counts, technically. Just trying to be helpful to the asker, they're asking why the array `a` got trashed. In the given gcc version and the given OS, I think what I gave was a fair explanation. – user3392439 Feb 24 '16 at 18:39