SE250:lab-2:hlin079
lab 2
This lab is about generating results on both windows and linux to understand the memory difference used for each c compiler.
task1
int *1p; char *2p; float *3p; double *4p; short *5p; long *6p;
printf( "sizeof( int* ) = %d\n", sizeof( 1p ) ); printf( "sizeof( char* ) = %d\n", sizeof( 2p ) ); printf( "sizeof( float* ) = %d\n", sizeof( 3p ) ); printf( "sizeof( double* ) = %d\n", sizeof( 4p ) ); printf( "sizeof( short* ) = %d\n", sizeof( 5p ) ); printf( "sizeof( long* ) = %d\n", sizeof( 6p ) );
All the pointers have the same size for both windows and linux. the other data types in c have the same size as int, long, char,float.
task2
int x; int y; printf("&x=%p,&y=%p, diff=%1d\n",&x,&y,(long)(&x-&y)); printf("&x=%p,&y=%p, diff=%1d\n",&x,&y,((long)&x-(long)&y));
For the windows &x=0x22ccbc,&y=0x22ccb4, diff=2 &x=0x22ccbc,&y=0x22ccb4, diff=8 For the Linux &x=0xffa8374c,&y=0xffa83744, diff=2 &x=0xffa8374c,&y=0xffa83744, diff=8
The (long )&x-(long)&y gives the bits difference . The (long)&x-&y gives the interget difference.
task3
int x; char arr[4]; int y; int x2; char arr1[10]; int y2; printf("&x=%p,&y=%p, diff=%1d\n",&x,&y,(long)(&x-&y)); printf("&x=%p,&y=%p, diff=%1d\n",&x,&y,((long)&x-(long)&y)); printf("size of arr is %d\n", sizeof(arr)); printf("&arr=%p, arr+4=%p,&arr[4]=%p\n",&arr, arr+4,&arr[4]); printf("&x2=%p,&y2=%p, diff2=%1d\n",&x2,&y2,(long)(&x2-&y2)); printf("&x2=%p,&y2=%p, diff2=%1d\n",&x2,&y2,((long)&x2-(long)&y2)); printf("size of arr1 is %d\n", sizeof(arr1)); printf("&arr1=%p, arr1+4=%p,&arr1[4]=%p\n",&arr1, arr1+4,&arr1[4]);
For the windows i got
&x=0x22ccbc,&y=0x22ccb4, diff=2 &x=0x22ccbc,&y=0x22ccb4, diff=8 size of arr is 4 &arr=0x22ccb8, arr+4=0x22ccbc,&arr[4]=0x22ccbc &x2=0x22ccb0,&y2=0x22cc9c, diff2=5 &x2=0x22ccb0,&y2=0x22cc9c, diff2=20 size of arr1 is 10 &arr1=0x22cca0, arr1+4=0x22cca4,&arr1[4]=0x22cca4
For the linux i got
&x=0xffa8374c,&y=0xffa83744, diff=2 &x=0xffa8374c,&y=0xffa83744, diff=8 size of arr is 4 &arr=0xffa83748, arr+4=0xffa8374c,&arr[4]=0xffa8374c &x2=0xffa83740,&y2=0xffa8373c, diff2=1 &x2=0xffa83740,&y2=0xffa8373c, diff2=4 size of arr1 is 10 &arr1=0xffa83752, arr1+4=0xffa83756,&arr1[4]=0xffa83756
task4
int x3; char arr2[4]; int y3; int main(){
printf("&x3=%p,&y3=%p, diff3=%1d\n",&x3,&y3,(long)(&x3-&y3)); printf("&x3=%p,&y3=%p, diff3=%1d\n",&x,&y,((long)&x3-(long)&y3)); printf("size of arr2 is %d\n", sizeof(arr2)); printf("&arr2=%p, arr2+4=%p,&arr2[4]=%p\n",&arr2, arr2+4,&arr2[4]);
return 0; }
For the windows:
&x3=0x403040,&y3=0x403030, diff3=4 &x3=0x22ccbc,&y3=0x22ccb4, diff3=16 size of arr2 is 4 &arr2=0x403020, arr2+4=0x403024,&arr2[4]=0x403024
For the linux:
&x3=0x10010d90,&y3=0x10010d94, diff3=-1 &x3=0xffa8374c,&y3=0xffa83744, diff3=-4 size of arr2 is 4 &arr2=0x10010d8c, arr2+4=0x10010d90,&arr2[4]=0x10010d90
I think the result for linux is negatve because linux works in the other order.
task5
int *p1, *p2; int q; p1 = &q; int r; p2 = &r; printf("p1 = %p, p2 = %p", p1, p2);
For windows:
p1=0x22ccbc, p2=0x22ccb8
For the linux:
p1=0xffc80754, p2=0xffc80750
task6
char *local_str() { char s[8] = "0123456"; return s; }
char *local_str2() { char s[8] = "abcdefg"; return s; }
char *static_str() { char s[8] = "tuvwxyz"; return s; } char *malloc_str() { char *s = malloc(8); strcpy(s, "hijklmn"); return s; } int main (void) { char *sp; sp = local_str( ); printf( "sp = %p(%s)\n", sp, sp ); trcpy(sp, "XXXXXXX"); printf("sp X'd = %p(%s)\n", sp, sp); sp = local_str(); local_str2(); printf( "sp = %p(%s)\n", sp, sp ); strcpy(sp, "XXXXXXX"); printf("sp X'd = %p(%s)\n", sp, sp); sp = static_str(); local_str2(); printf( "sp = %p(%s)\n", sp, sp ); strcpy(sp, "XXXXXXX"); printf("sp X'd = %p(%s)\n", sp, sp); sp = malloc_str(); local_str2(); printf( "sp = %p(%s)\n", sp, sp ); strcpy(sp, "XXXXXXX"); printf("sp X'd = %p(%s)\n", sp, sp); return 0; }
For the Windows: sp = 0x22cc90(456) sp X'd = 0x22cc90(-0@) sp = 0x22cc90(efg) sp X'd = 0x22cc90(-0@) sp = 0x402000(tuvwxyz) sp X'd = 0x402000(XXXXXXX) sp = 0xda0170(hijklmn) sp X'd = 0xda0170(XXXXXXX) For Linux: sp = 0xffaa1724( þîÐ►) sp X'd = 0xffaa1724(XXXXXXX) sp = 0xffaa1724(abcdefg) sp X'd = 0xffaa1724(XXXXXXX) sp = 0x10010dc0(tuvwxyz) sp X'd = 0x10010dc0(XXXXXXX) sp = 0x10011008(hijklmn) sp X'd = 0x10011008(XXXXXXX)
task7
typedef struct{ char my_char; short my_short; int my_int; long my_long; float my_float; double my_double; }my_struct; int main(){ my_struct as; printf("&my_struct=%p\n", &as); printf("offsets:\n" "my_char:%1d\n" "my_short:%1d\n" "my_int:%1d\n" "my_long:%1d\n" "my_float:%1d\n" "my_double:%1d\n", (long)&as-(long)&as.my_char, (long)&as-(long)&as.my_short, (long)&as-(long)&as.my_int, (long)&as-(long)&as.my_long, (long)&as-(long)&as.my_float, (long)&as-(long)&as.my_double); return 0; }
Results: Windows: offsets: my_char: 0 my_short: -2 my_int: -4 my_long: -8 my_float: -12 my_double: -16
Linux: &my struct = 0xffae2740 offsets: my_char: 0 my_short: -2 my_int: -4 my_long: -8 my_float: -12 my_double: -16
task8
typedef union{ char my_char; short my_short; int my_int; long my_long; float my_float; double my_double; }my_struct; int main(){ my_struct as; printf("&my_struct=%p\n", &as); printf("offsets:\n" "my_char:%1d\n" "my_short:%1d\n" "my_int:%1d\n" "my_long:%1d\n" "my_float:%1d\n" "my_double:%1d\n", (long)&as-(long)&as.my_char, (long)&as-(long)&as.my_short, (long)&as-(long)&as.my_int, (long)&as-(long)&as.my_long, (long)&as-(long)&as.my_float, (long)&as-(long)&as.my_double); return 0; }
Results: Windows: offsets: my_char: 0 my_short: 0 my_int: 0 my_long: 0 my_float: 0 my_double: 0
Linux: offsets: my_char: 0 my_short: 0 my_int: 0 my_long: 0 my_float: 0 my_double: 0 I think the union saved all the data in the same space, but the struct saved each data in different space.
task9
int main (void) {
char *sp1, *sp2, *sp3; sp1 = malloc( 10 ); sp2 = malloc( 10 ); printf("Address of sp1 = %p. Address of sp2 - %p.\n", sp1, sp2); printf("sp1 = %s\n", sp1); free( sp1 ); sp1[6] = 'd'; printf("sp1 = %s\n", sp1); sp3 = malloc( 10 ); printf("Address of sp1 = %p. Address of sp2 - %p. Address of sp3 - %p\n", sp1, sp2, sp3);
return 0; }
Results: Windows: Address of sp1 = 0x1040198. Address of sp2 - 0x10401a8. sp1 = sp1 = ԝ aԝda Address of sp1 = 0x1040198. Address of sp2 - 0x10401a8. Address of sp3 - 0x1040198 Address of sp1 = 0xf70198. Address of sp2 - 0xf701a8. sp1 = sp1 = ԝ aԝda Address of sp1 = 0xf70198. Address of sp2 - 0xf701a8. Address of sp3 - 0xf70198 Address of sp1 = 0xd90198. Address of sp2 - 0xd901a8. sp1 = sp1 = ԝ aԝda Address of sp1 = 0xd90198. Address of sp2 - 0xd901a8. Address of sp3 - 0xd90198 Address of sp1 = 0xd90198. Address of sp2 - 0xd901a8. sp1 = sp1 = ԝ aԝda Address of sp1 = 0xd90198. Address of sp2 - 0xd901a8. Address of sp3 - 0xd90198 Address of sp1 = 0xfa0198. Address of sp2 - 0xfa01a8. sp1 = sp1 = ԝ aԝda Address of sp1 = 0xfa0198. Address of sp2 - 0xfa01a8. Address of sp3 - 0xfa0198
Linux: Address of sp1 = 0x10011008. Address of sp2 - 0x10011018. sp1 = sp1 = Address of sp1 = 0x10011008. Address of sp2 - 0x10011018. Address of sp3 - 0x10 011008
At the beginning the sp1 and sp3 have their own memory location but after sp1 was freed the address of memory sp1 was pointing to where sp3 was pointing to.
task10
For the windows: 0x401050
For the linux: 0x100004dc