SE250:lab-2:hpan027
Lab Report
- Is it just me or does this lab take forever?
1.
#include <stdio.h> int main(void) { int* int_pointer; double* double_pointer; float* float_pointer; long* long_pointer; short* short_pointer; int int_variable; double double_variable; float float_variable; long long_variable; short short_variable; printf( "%d %d %d %d %d\n", sizeof(int_pointer), sizeof(double_pointer), sizeof(float_pointer), sizeof(long_pointer), sizeof(short_pointer) ); printf( "%d %d %d %d %d", sizeof(int_variable), sizeof(double_variable), sizeof(float_variable), sizeof(long_variable), sizeof(short_variable) ); return 1; }
Returns (for both Windows and Linux)
4 4 4 4 4 4 8 4 4 2
From this we can see:
- All pointers are the same size, regardless of the variable type they point to
- Pointers and variables of type int, float and long share the same size
- Double variables are twice the size of pointers
- Short variables are half the size
2.
#include <stdio.h> int main(void) { int x; 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) ); return 1; }
Output (Windows):
&x = 0x22cca4, &y = 0x22cca0, diff = 1 &x = 0x22cca4, &y = 0x22cca0, diff = 4
Output (Linux):
&x = 0xffa159dc, &y = 0xffa159d8, diff = 1 &x = 0xffa159dc, &y = 0xffa159d8, diff = 4
- It seems that putting (long) in front of both variables make C treat this purely as a numerical calculation, where the difference between two numbers is calculated
- Putting (long) in front of the whole expression make C take into consideration the type of the variable, hence they are calculated to be one "int" apart
- Linux seems to have a completely different set of memory addresses, but the difference remains the same
3.
Output with arr[4] (Windows):
&x = 0x22cca4, &y = 0x22cc9c, diff(bytes) = 8 size of array = 4 &arr = 0x22cca0, arr+4 = 0x22cca4, &arr[4] = 0x22cca4
Output with arr[4] (Linux):
&x = 0xffe709dc, &y = 0xffe709d4, diff(bytes) = 8 size of array = 4 &arr = 0xffe709d8, arr+4 = 0xffe709dc, &arr[4] = 0xffe709dc
- The size of the array is 4 bytes
- &arr is the address of the first element in the array
- arr+4 and &arr[4] refers to the same thing: the address 4 bytes from the first element in arr - which in this case is where the variable x is stored
- Difference between &x and &y is 8 bytes
- Again, the only difference with running Linux is the part of memory the variables are stored in
Output for arr[0] - arr[10] (Windows):
arr[0], diff (bytes) = 32 arr[1], diff (bytes) = 8 arr[2], diff (bytes) = 8 arr[3], diff (bytes) = 32 arr[4], diff (bytes) = 8 arr[5], diff (bytes) = 32 arr[6], diff (bytes) = 32 arr[7], diff (bytes) = 32 arr[8], diff (bytes) = 16 arr[9], diff (bytes) = 32 arr[10], diff (bytes) = 32
Output for arr[0] - arr[10] (Linux):
arr[0], diff (bytes) = 4 arr[1], diff (bytes) = 4 arr[2], diff (bytes) = 4 arr[3], diff (bytes) = 4 arr[4], diff (bytes) = 8 arr[5], diff (bytes) = 4 arr[6], diff (bytes) = 4 arr[7], diff (bytes) = 4 arr[8], diff (bytes) = 4 arr[9], diff (bytes) = 4 arr[10], diff (bytes) = 4
- The Linux results were initially very confusing
- Upon closer inspection of the memory addresses, it seems that arr is not always stored between x and y. In fact, the only time it is stored between x and y is when the size of the array if 4. For all other instances, x and y is stored consecutively and arr is moved to either before or after the pair
- Windows seems to dump arr between x and y regardless of the size of the array
4.2
Windows
&x = 0x403030, &y = 0x403020, diff(byte) = 16
- x and y are now global variables, and are stored in a completely different area of memory
- There is now also a greater gap between the two variables
Linux
&x = 0x10010a68, &y = 0x10010a6c, diff(byte) = -4
- Variable order swapped around
- Only 4 bytes apart - same as when they were local variables
4.3
Windows
&x = 0x403040, &y = 0x403020, diff(bytes) = 32 size of array = 4 &arr = 0x403030, arr+4 = 0x403034, &arr[4] = 0x403034 arr[4], diff (bytes) = 32
- Again, different area of memory
- 32 bytes in between (each variable seems to be taking 16 bytes of memory)
Linux
&x = 0x10010b38, &y = 0x10010b40, diff(bytes) = -8 size of array = 4 &arr = 0x10010b3c, arr+4 = 0x10010b40, &arr[4] = 0x10010b40 arr[4], diff (bytes) = -8
- Different area of memory
- No extra usage of memory
- Order reversed
Output for arr[0] - arr[10] (Windows):
arr[0], diff (bytes) = 32 arr[1], diff (bytes) = 32 arr[2], diff (bytes) = 32 arr[3], diff (bytes) = 32 arr[4], diff (bytes) = 32 arr[5], diff (bytes) = 32 arr[6], diff (bytes) = 32 arr[7], diff (bytes) = 32 arr[8], diff (bytes) = 32 arr[9], diff (bytes) = 32 arr[10], diff (bytes) = 32
- Windows seems to be pretty consistent here, with 32 bytes between x and y regardless of the size of the array
Output for arr[0] - arr[10] (Linux):
arr[0], diff (bytes) = -4 arr[1], diff (bytes) = -8 arr[2], diff (bytes) = -8 arr[3], diff (bytes) = -8 arr[4], diff (bytes) = -8 arr[5], diff (bytes) = -12 arr[6], diff (bytes) = -12 arr[7], diff (bytes) = -12 arr[8], diff (bytes) = -12 arr[9], diff (bytes) = -4 arr[10], diff (bytes) = -4
- When the length of the array is 0, arr[0] and y shares the same addresses - i.e. the array isn't allocated any memory
- For array length 1-8, the array is stored between x and y
- For array length 9-10, x and y are stored consecutively, and arr is shuffled to the end
5.
- p1 and p2 stores the address of q and r respectively
6.
Windows
sp = 0x22cc70(456) sp = 0x22cc70(efg) sp = 0x402000(tuvwxyz) sp = 0x1062100(hijklmn)
- Parts of the string seems to have been lost with the first two instances. This is probably due to local variables not being retained after the function finishes running
- Also local, static and malloc uses different parts of memory as shown
Linux
sp = 0xffcb09a4(�þîÐ�) sp = 0xffcb09a4(abcdefg) sp = 0x10010cc4(tuvwxyz) sp = 0x10011008(hijklmn)
- Local, static and malloc uses different parts of memory as in Windows
- For some reason the second local string is retained while the first is lost completely!?
Adding
strcpy( sp, "XXXXXXX"); printf( "sp X'd = %p(%s)\n", sp, sp);
to the code
Windows:
sp = 0x22cc70(456) sp X'd = 0x22cc70(-0@) sp = 0x22cc70(efg) sp X'd = 0x22cc70(-0@) sp = 0x402000(tuvwxyz) sp X'd = 0x402000(XXXXXXX) sp = 0xda2100(hijklmn) sp X'd = 0xda2100(XXXXXXX)
- Can't seem to X the area of memory where the local variable was stored
Linux:
sp = 0xffa399a4(�þîÐ�) sp X'd = 0xffa399a4(XXXXXXX) sp = 0xffa399a4(abcdefg) sp X'd = 0xffa399a4(XXXXXXX) sp = 0x10010db0(tuvwxyz) sp X'd = 0x10010db0(XXXXXXX) sp = 0x10011008(hijklmn) sp X'd = 0x10011008(XXXXXXX)
- Can seem to X the area of memory where the local variable was stored
7.
Output (Windows):
&my_struct = 0x403020 offsets: my_char: 0 my_short: -2 my_int: -4 my_long: -8 my_float: -12 my_double: -16
output (Linux):
&my_struct = 0x10010ac0 offsets: my_char: 0 my_short: -2 my_int: -4 my_long: -8 my_float: -12 my_double: -16
- Identical offsets to Windows
8.
Windows:
&my_struct = 0x403020 offsets: my_char: 0 my_short: 0 my_int: 0 my_long: 0 my_float: 0 my_double: 0
Linux:
&my_struct = 0x10010ab8 offsets: my_char: 0 my_short: 0 my_int: 0 my_long: 0 my_float: 0 my_double: 0
- Unlike struct, there doesn't seem to be any space preallocated for each variable
- Linux and Windows have same output barring difference in memory address
9.
Windows:
*sp1 = 0xd920f0 *sp2 = 0xda2108 *sp3 = 0xd920f0
- An area of memory that's freed will be reused subsequently
Linux:
*sp1 = 0x10011008 *sp2 = 0x10011018 *sp3 = 0x10011008
- This is also the case for Linux
10.
Using the code from 6)
Windows:
local_str = 0x401050 sp = 0x22cc70(456) sp X'd = 0x22cc70(=0@) sp = 0x22cc70(efg) sp X'd = 0x22cc70(=0@) sp = 0x402000(tuvwxyz) sp X'd = 0x402000(XXXXXXX) sp = 0xda20f8(hijklmn) sp X'd = 0xda20f8(XXXXXXX)
- Seems to be somewhat close to where the static variables are stored
Linux:
local_str = 0x100004dc sp = 0xffe8f9a4(0123456) sp X'd = 0xffe8f9a4(XXXXXXX) sp = 0xffe8f9a4(abcdefg) sp X'd = 0xffe8f9a4(XXXXXXX) sp = 0x10010df0(tuvwxyz) sp X'd = 0x10010df0(XXXXXXX) sp = 0x10011008(hijklmn) sp X'd = 0x10011008(XXXXXXX)
- Near static variables and malloc space
Hpan027 15:56, 11 March 2008 (NZDT)