SE250:lab-2:dols008

From Marks Wiki
Jump to navigation Jump to search

Task 1

Windows results:

sizeof(ip) = 4;

Linux results:

sizeof(ip) = 4;


Answers:

All pointers that I tried are the same size. Long, float and int are all the same size on the windows machine. I didn't try everything to save time.


Task 2

Windows results:

&x = 0x22ccb8, &y = 0x22ccb0
(long)(&x - &y) = 1
(long)&x - (long)&y = 4

(long)(&x - &y) finds the difference between the addresses in unit of sizeof(int), whereas (long)&x - (long)&y finds the difference in bytes. Linux results were similar, with different addresses.


Task 3

&arr = 0x22ccb4

arr+4 = 0x22ccb8

&arr[4] = 0x22ccb8

So arr+4 and &arr[4] are equivalent.

When the array size increased to 10, the difference between &x and &y increased to 32 bytes. Which is odd, considering 16 is a nice round number closer to the 14 actually required. In linux, the compiler shifted the array such that x and y were next to each other, with a difference of 4. Writing past the end of the array (arr[4]) overwrote x.


Task 4

I'll keep it short. For the most part globals were similar, but in a seperate memory area. Global variables tend to be further apart like 16 bytes between the integers.


Task 5

The values of p1 and p2 are the addresses of q and r. Am I missing something?


Task 6

Printing the local strings which have gone out of scope gave unpredictable results. On the windows machine the first string became 456, and the second one became efg because it was overwritten by the call to local_str2. In linux the first string became garbage, and the second became abcdefg. The other strings were as expected.


Task 7

Struct

offsets:
my_char: 0
my_short: -2
my_int: -4
my_long: -8
my_float: -12
my_double: -16

Task 8

Union

offsets:
my_char: 0
my_short: 0
my_int: 0
my_long: 0
my_float: 0
my_double: 0

Unions store all their variables in the same memory location. This is useful to to interpret the same piece of memory as several different types.


Task 9

sp3 is allocated the same piece of memory as sp1, because sp1 was free'd. If sp1 is used it will overwrite the data sp3 is pointing to.


Task 10

Windows:

&local_str = 0x401050

Linux:

&local_str = 0x100004ac

Dols008 19:34, 11 March 2008 (NZDT)