SE250:lab-2:rwan064
Introduction
I am sick with the flu so doing the lab exercise on my laptop at home instead of the GCL lab computers. I also managed to log into the Linux server using PuTTY so got some results for Linux too.
Results
Results from different compilers and environments
Windows Vista: cygwin
These are the results from Vista Business on my laptop. Specs on my laptop:
- Intel Core 1.66 GHz
- 1 GB RAM
That should be enough.
Task 1
int *ip; char *cp; float *fp; double *dp; short *sp; long *lp; printf( "sizeof( int* ) = %d\n", sizeof( ip ) ); printf( "sizeof( char* ) = %d\n", sizeof( cp ) ); printf( "sizeof( float* ) = %d\n", sizeof( fp ) ); printf( "sizeof( double* ) = %d\n", sizeof( dp ) ); printf( "sizeof( short* ) = %d\n", sizeof( sp ) ); printf( "sizeof( long* ) = %d\n", sizeof( lp ) );
Results:
$ ./main.exe sizeof( int* ) = 4 sizeof( char* ) = 4 sizeof( float* ) = 4 sizeof( double* ) = 4 sizeof( short* ) = 4 sizeof( long* ) = 4
- Are all pointers the same size? YES
- What other data types in C are the same size? int
Task 2
int x, y; printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)(&x - &y) ); printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)&x - (long)&y );
Results:
$ ./main.exe &x = 0x22cca4, &y = 0x22cca0, diff = 1 &x = 0x22cca4, &y = 0x22cca0, diff = 4
- What is the difference between (long)&x - (long)&y and (long)(&x - &y)?
Task 3
int x; char arr[4]; int y; printf( "sizeof( arr ) = %d\n", sizeof( arr ) ); printf( "&arr = %p\n", &arr ); printf( "arr+4 = %p\n", arr+4 ); printf( "&arr[4] = %p\n", &arr[4] )
Results:
sizeof( arr ) = 4 &arr = 0x22cca0 arr+4 = 0x22cca4 &arr[4] = 0x22cca4
What does sizeof report as the size of the array? The number of elements in the array
Differences between &x and &y for varing size of arr:
int x; char arr[0]; int y; printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)(&x - &y) ); printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)&x - (long)&y );
Results:
For sizeof( arr ) = 0 ---------------------------- &x = 0x22cc9c, &y = 0x22cc7c, diff = 8 &x = 0x22cc9c, &y = 0x22cc7c, diff = 32 For sizeof( arr ) = 1 ---------------------------- &x = 0x22cca4, &y = 0x22cc9c, diff = 2 &x = 0x22cca4, &y = 0x22cc9c, diff = 8 For sizeof( arr ) = 2 ---------------------------- &x = 0x22cca4, &y = 0x22cc9c, diff = 2 &x = 0x22cca4, &y = 0x22cc9c, diff = 8 For sizeof( arr ) = 3 ---------------------------- &x = 0x22cc9c, &y = 0x22cc7c, diff = 8 &x = 0x22cc9c, &y = 0x22cc7c, diff = 32 For sizeof( arr ) = 4 ---------------------------- &x = 0x22cca4, &y = 0x22cc9c, diff = 2 &x = 0x22cca4, &y = 0x22cc9c, diff = 8 For sizeof( arr ) = 5 ---------------------------- &x = 0x22cc9c, &y = 0x22cc7c, diff = 8 &x = 0x22cc9c, &y = 0x22cc7c, diff = 32 For sizeof( arr ) = 6 ---------------------------- &x = 0x22cc9c, &y = 0x22cc7c, diff = 8 &x = 0x22cc9c, &y = 0x22cc7c, diff = 32 For sizeof( arr ) = 7 ---------------------------- &x = 0x22cc9c, &y = 0x22cc7c, diff = 8 &x = 0x22cc9c, &y = 0x22cc7c, diff = 32 For sizeof( arr ) = 8 ---------------------------- &x = 0x22cca4, &y = 0x22cc94, diff = 4 &x = 0x22cca4, &y = 0x22cc94, diff = 16 For sizeof( arr ) = 9 ---------------------------- &x = 0x22cc9c, &y = 0x22cc7c, diff = 8 &x = 0x22cc9c, &y = 0x22cc7c, diff = 32 For sizeof( arr ) = 10 ---------------------------- &x = 0x22cc9c, &y = 0x22cc7c, diff = 8 &x = 0x22cc9c, &y = 0x22cc7c, diff = 32
Putting values into the variables:
int x = 0; char arr[4]; int y = 0; arr[4] = 10; printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)(&x - &y) ); printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)&x - (long)&y ); printf( "x = %d, y = %d\n", x, y );
Results:
$ ./main.exe &x = 0x22cca4, &y = 0x22cc9c, diff = 2 &x = 0x22cca4, &y = 0x22cc9c, diff = 8 x = 10, y = 0
Task 4
Differences between &x and &y for varying sizes of arr:
int x; int y; int main( void ) { char arr[0]; printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)(&x - &y) ); printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)&x - (long)&y ); return 0; }
Results:
For sizeof( arr ) = 0 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 1 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 2 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 3 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 4 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 5 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 6 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 7 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 8 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 9 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 For sizeof( arr ) = 10 ---------------------------- &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16
Putting values into variables:
int x; int y; int main( void ) { char arr[4]; arr[4] = 10; x = 0; y = 0; printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)(&x - &y) ); printf( "&x = %p, &y = %p, diff = %ld\n", &x, &y, (long)&x - (long)&y ); printf( "x = %d, y = %d\n", x, y ); return 0; }
Results:
$ ./main.exe &x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16 x = 0, y = 0
Task 5
int *p1, *p2; printf( "Un-initializd pointers:\n" ); printf( "p1 = %p, p2 = %p\n", p1, p2 ); printf( "\n" ); { int q; p1 = &q; printf( "Inside first scope:\n" ); printf( "&q = %p\n", &q ); printf( "p1 = %p, p2 = %p\n", p1, p2 ); printf( "\n" ); } { int r; p2 = &r; printf( "Inside second scope:\n" ); printf( "&r = %p\n", &r ); printf( "p1 = %p, p2 = %p\n", p1, p2 ); printf( "\n" ); } printf( "p1 = %p, p2 = %p\n", p1, p2 ); printf( "*p1 = %d, *p2 = %d\n", *p1, *p2 );
Results:
$ ./main.exe Un-initializd pointers: p1 = 0x401450, p2 = 0x611021a0 Inside first scope: &q = 0x22ccbc p1 = 0x22ccbc, p2 = 0x611021a0 Inside second scope: &r = 0x22ccb8 p1 = 0x22ccbc, p2 = 0x22ccb8 p1 = 0x22ccbc, p2 = 0x22ccb8 *p1 = 0, *p2 = 1628704768
Task 6
Results:
$ ./main.exe sp = 0x22cc90(456) sp = 0x22cc90(efg) sp = 0x402000(tuvwxyz) sp = 0xda0170(hijklmn)
Results with extra printf strcpy and printf:
$ ./main.exe 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)
Task 7
Results:
$ ./main.exe &my_struct = 0x0 offsets: my_char: 0 my_short: -2 my_int: -4 my_long: -8 my_float: -12 my_double: -16
Task 8
Results:
$ ./main.exe &my_struct = 0x0 offsets: my_char: 0 my_short: 0 my_int: 0 my_long: 0 my_float: 0 my_double: 0
Task 9
char *sp1, *sp2, *sp3; printf( "Declared\nsp1 = %p, sp2 = %p, sp3 = %p\n\n", sp1, sp2, sp3 ); sp1 = malloc( 10 ); printf( "sp1 mallocd\nsp1 = %p, sp2 = %p, sp3 = %p\n\n", sp1, sp2, sp3 ); sp2 = malloc( 10 ); printf( "sp2 mallocd\nsp1 = %p, sp2 = %p, sp3 = %p\n\n", sp1, sp2, sp3 ); free( sp1 ); printf( "sp1 freed\nsp1 = %p, sp2 = %p, sp3 = %p\n\n", sp1, sp2, sp3 ); sp3 = malloc( 10 ); printf( "sp3 mallocd\nsp1 = %p, sp2 = %p, sp3 = %p\n\n", sp1, sp2, sp3 );
Results:
$ ./main.exe Declared sp1 = 0x401420, sp2 = 0x611021a0, sp3 = 0x0 sp1 mallocd sp1 = 0x1030170, sp2 = 0x611021a0, sp3 = 0x0 sp2 mallocd sp1 = 0x1030170, sp2 = 0x1030180, sp3 = 0x0 sp1 freed sp1 = 0x1030170, sp2 = 0x1030180, sp3 = 0x0 sp3 mallocd sp1 = 0x1030170, sp2 = 0x1030180, sp3 = 0x1030170
- What can you say about the pointers returned by malloc?
- What will happen if sp1 is used after it has been freed?
Task 10
Results:
$ ./main.exe local_str = 0x401050
Linux server
Task 1
sizeof( int* ) = 4 sizeof( char* ) = 4 sizeof( float* ) = 4 sizeof( double* ) = 4 sizeof( short* ) = 4 sizeof( long* ) = 4
Task 2
&x = 0xffd4278c, &y = 0xffd42788, diff = 1 &x = 0xffd4278c, &y = 0xffd42788, diff = 4
Task 3
sizeof( arr ) = 4 &arr = 0xffd23784 arr+4 = 0xffd23788 &arr[4] = 0xffd23788
&x = 0xffdf878c, &y = 0xffdf8788, diff = 1 &x = 0xffdf878c, &y = 0xffdf8788, diff = 4 &x = 0xffaa9788, &y = 0xffaa9784, diff = 1 &x = 0xffaa9788, &y = 0xffaa9784, diff = 4 &x = 0xfff0b788, &y = 0xfff0b784, diff = 1 &x = 0xfff0b788, &y = 0xfff0b784, diff = 4 &x = 0xfface788, &y = 0xfface784, diff = 1 &x = 0xfface788, &y = 0xfface784, diff = 4 &x = 0xffebc78c, &y = 0xffebc784, diff = 2 &x = 0xffebc78c, &y = 0xffebc784, diff = 8 &x = 0xffcb078c, &y = 0xffcb0788, diff = 1 &x = 0xffcb078c, &y = 0xffcb0788, diff = 4 &x = 0xff81878c, &y = 0xff818788, diff = 1 &x = 0xff81878c, &y = 0xff818788, diff = 4 &x = 0xffc8178c, &y = 0xffc81788, diff = 1 &x = 0xffc8178c, &y = 0xffc81788, diff = 4 &x = 0xffd9f780, &y = 0xffd9f77c, diff = 1 &x = 0xffd9f780, &y = 0xffd9f77c, diff = 4 &x = 0xfff2077c, &y = 0xfff20778, diff = 1 &x = 0xfff2077c, &y = 0xfff20778, diff = 4 &x = 0xfff3077c, &y = 0xfff30778, diff = 1 &x = 0xfff3077c, &y = 0xfff30778, diff = 4
&x = 0xffd4178c, &y = 0xffd41784, diff = 2 &x = 0xffd4178c, &y = 0xffd41784, diff = 8 x = 167772160, y = 0
Task 4
&x = 0x10010aa4, &y = 0x10010aa8, diff = -1 &x = 0x10010aa4, &y = 0x10010aa8, diff = -4 &x = 0x10010aa4, &y = 0x10010aa8, diff = -1 &x = 0x10010aa4, &y = 0x10010aa8, diff = -4 &x = 0x10010aa4, &y = 0x10010aa8, diff = -1 &x = 0x10010aa4, &y = 0x10010aa8, diff = -4 &x = 0x10010aa4, &y = 0x10010aa8, diff = -1 &x = 0x10010aa4, &y = 0x10010aa8, diff = -4 &x = 0x10010aa4, &y = 0x10010aa8, diff = -1 &x = 0x10010aa4, &y = 0x10010aa8, diff = -4 &x = 0x10010aa4, &y = 0x10010aa8, diff = -1 &x = 0x10010aa4, &y = 0x10010aa8, diff = -4 &x = 0x10010aa4, &y = 0x10010aa8, diff = -1 &x = 0x10010aa4, &y = 0x10010aa8, diff = -4 &x = 0x10010aa4, &y = 0x10010aa8, diff = -1 &x = 0x10010aa4, &y = 0x10010aa8, diff = -4 &x = 0x10010b18, &y = 0x10010b1c, diff = -1 &x = 0x10010b18, &y = 0x10010b1c, diff = -4 &x = 0x10010b18, &y = 0x10010b1c, diff = -1 &x = 0x10010b18, &y = 0x10010b1c, diff = -4 &x = 0x10010b18, &y = 0x10010b1c, diff = -1 &x = 0x10010b18, &y = 0x10010b1c, diff = -4
&x = 0x10010af4, &y = 0x10010af8, diff = -1 &x = 0x10010af4, &y = 0x10010af8, diff = -4 x = 0, y = 0
Task 5
Un-initializd pointers: p1 = 0xff929a24, p2 = 0x1 Inside first scope: &q = 0xff929784 p1 = 0xff929784, p2 = 0x1 Inside second scope: &r = 0xff929784 p1 = 0xff929784, p2 = 0xff929784 p1 = 0xff929784, p2 = 0xff929784 *p1 = -7169520, *p2 = -7169520
Task 6
sp = 0xffb01754(�þîÐ�) sp = 0xffb01754(abcdefg) sp = 0x10010cd4(tuvwxyz) sp = 0x10011008(hijklmn)
sp = 0xffcf4754(�þîÐ�) sp X’d = 0xffcf4754(XXXXXXX) sp = 0xffcf4754(abcdefg) sp X’d = 0xffcf4754(XXXXXXX) sp = 0x10010dc0(tuvwxyz) sp X’d = 0x10010dc0(XXXXXXX) sp = 0x10011008(hijklmn) sp X’d = 0x10011008(XXXXXXX)
Task 7
&my_struct = 0xffab5770 offsets: my_char: 0 my_short: -2 my_int: -4 my_long: -8 my_float: -12 my_double: -16
Task 8
&my_struct = 0xffec6788 offsets: my_char: 0 my_short: 0 my_int: 0 my_long: 0 my_float: 0 my_double: 0
Task 9
Declared sp1 = 0xffa49a10, sp2 = 0xffa49a24, sp3 = 0x1 sp1 mallocd sp1 = 0x10011008, sp2 = 0xffa49a24, sp3 = 0x1 sp2 mallocd sp1 = 0x10011008, sp2 = 0x10011018, sp3 = 0x1 sp1 freed sp1 = 0x10011008, sp2 = 0x10011018, sp3 = 0x1 sp3 mallocd sp1 = 0x10011008, sp2 = 0x10011018, sp3 = 0x10011008
Task 10
local_str = 0x100004ac
Memory Map
local variables 0x22CCA4 0x22CCA0 static variables 0x403030 0x403020 malloc'd variables 0x402000 0xDA0170
Image of memory locations: <html> <embed src="http://www.rajithaonline.com/SE250/map.png" width="270" height="494" /> </html>
Conclusions & Discussion
I had to read through the lab instructions a couple of times till I understood properly exactly what I need to do. It was quite a lot of work re-typing, compiling and running multiple times. I started off with using Cygwin to run the programs on my Windows machine. This wasn't too hard for many of the exercises since all I need to do was copy and paste the code and run it. But it was quite annoying doing Task 3 and 4 because I had to edit, compile and run MANY times!
For the Linux machine I logged into the server (login.cs.auckland.ac.nz) using PuTTY. The problem I had now was to find a way to easily get the results from the server. I don't want to have to edit, compile and run about 20 times because I will also have to upload the edited file that many times since I was at home. So I made multiple files which has everything I need to run. Then I wrote a shell script to automatically compile and run all of the files and output the results into a file instead of the screen.
But overall the lab wasn't too challenging but it was a lot of work.