mauro mauro - 4 years ago 94
C Question

Copying strings containing longs

I'm able to "pack" arrays of long in strings. Now, if I memcpy the string containing arrays of long, the content is lost. Here you have my code:

#include <stdio.h>
#include <string.h>

int main()
{
unsigned char arr[40];
unsigned char arr2[40];
unsigned int i = 0 ;
long f=0;

for ( i = 0 ; i < 5 ; i++ ) {
f = i + 1 ;
*((long *)arr + i*sizeof(long)) = f ;
}
memcpy(arr2, arr, 40);
for ( i = 0 ; i < 5 ; i++ ) {
f = *((long *)arr2 + i*sizeof(long)) ;
printf("f =%ld from arr2 sizeof %zu\n", f, (size_t)(i*sizeof(long)));
}
for ( i = 0 ; i < 5 ; i++ ) {
f = *((long *)arr + i*sizeof(long)) ;
printf("f =%ld from arr sizeof %zu\n", f, (size_t)(i*sizeof(long)));
}
}


And this is the output:

f =1 from arr2 sizeof 0
f =0 from arr2 sizeof 8
f =1 from arr2 sizeof 16
f =140734674541394 from arr2 sizeof 24
f =140734674541702 from arr2 sizeof 32
f =1 from arr sizeof 0
f =2 from arr sizeof 8
f =3 from arr sizeof 16
f =4 from arr sizeof 24
f =5 from arr sizeof 32


As you can see I'm able to read the long values I did store in "arr" but not from "arr2". Any clue?

Answer Source

When you add 1 to pointer p and sizeof(*p) == 8, it's equivalent to adding 8 to a char pointer. In other words, once you have cast the pointer, there's no need to scale the index anymore.

#include <stdio.h>
#include <string.h>

int main() 
{
    unsigned char arr[40];
    unsigned char arr2[40];
    unsigned int i = 0 ;
    long f=0;

    for ( i = 0 ; i < 5 ; i++ ) {
        f = i + 1 ;
        *((long *)arr + i) = f ;
    }
    memcpy(arr2, arr, 40);
    for ( i = 0 ; i < 5 ; i++ ) {
        f = *((long *)arr2 + i) ;
        printf("f =%ld from arr2 sizeof %zu\n", f, (size_t)(i*sizeof(long)));
    }
    for ( i = 0 ; i < 5 ; i++ ) {
        f = *((long *)arr + i) ;
        printf("f =%ld from arr sizeof %zu\n", f, (size_t)(i*sizeof(long)));
    }
}   

Technically though, your program is still undefined because it violates strict aliasing. You could solve that by using an array of long and accessing it via a char pointer or by using dynamic memory instead of stack-allocated arrays.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download