SE250:lab-2:stsa027
1.
All pointers are the same size. The types int, short, long, double, float all have the same size of 4 bytes. The code given in the lab-2 worksheet was used. When the same is done on the Linux server, the same results were found - the size of the pointers are all 4 bytes.
2.
On lab machines:
&x = 0x22cca4, &y = 0x22cca0, diff = 1 &x = 0x22cca4, &y = 0x22cca0, diff = 4
On the Linux server:
&x = 0xff91e748, &y = 0xff91e744, diff = 1 &x = 0xff91e748, &y = 0xff91e744, diff = 4
Using either methods, the address returned is identical, as expected. The addresses of x and y are adjacent in memory, shown by the difference of 4 between the addresses. However, long(&x - &y) gives a value of 1, while long(&x) - long(&y) gives the expected value of 4 bytes. This was explained by the fact that the unit of the first value returned is the 'size of an int', which caused the first value to be 4 times to small (sizeof(int) = 4 bytes). The memory used was local
3.
With an int array of size 4 between the x and y variables
first on the lab computers:
array size= 4, &arr = 0x22cc94, &(arr+4)=0x22cc98, diff = -4 &x1 = 0x22cca0, &y1 = 0x22cc9c, diff = 8
then on the Linux server:
array size= 4, &arr = 0xff91e73c, &(arr+4)=0xff91e740, diff = -4 &x1 = 0xff91e748, &y1 = 0xff91e744, diff = 8
The only difference seen when the task was performed on the Linux server was the address locations (and size of addresses, which is expected, since the Linux server is 64-bits). While the lab computers gave local variables addresses at the beginning of memory (0x22cc9c), Linux allocated memory for the local variables at the end of memory (0xff91e744).
The following are results obtained from the Lab machines, with a changing array size:
array size= 0, &arr = 0x22cc80 &x1 = 0x22cc90, &y1 = 0x22cc7c, diff = 20 array size= 1, &arr = 0x22cc97 &x1 = 0x22cc98, &y1 = 0x22cc90, diff = 8 array size= 2, &arr = 0x22cc96 &x1 = 0x22cc98, &y1 = 0x22cc90, diff = 8 array size= 3, &arr = 0x22cc80 &x1 = 0x22cc90, &y1 = 0x22cc7c, diff = 20 array size= 4, &arr = 0x22cc94 &x1 = 0x22cc98, &y1 = 0x22cc90, diff = 8 array size= 5, &arr = 0x22cc80 &x1 = 0x22cc90, &y1 = 0x22cc7c, diff = 20 array size= 6, &arr = 0x22cc80 &x1 = 0x22cc90, &y1 = 0x22cc7c, diff = 20 array size= 7, &arr = 0x22cc80 &x1 = 0x22cc90, &y1 = 0x22cc7c, diff = 20 array size= 8, &arr = 0x22cc90 &x1 = 0x22cc98, &y1 = 0x22cc8c, diff = 12 array size= 9, &arr = 0x22cc80 &x1 = 0x22cc90, &y1 = 0x22cc7c, diff = 20 array size= 10, &arr = 0x22cc80 &x1 = 0x22cc90, &y1 = 0x22cc7c, diff = 20
There seems to be something unusual occurring. It might be expected that there would be a gradual increase in the difference seen in address distance as array size increases. However this is not the case. The difference in addresses of x and y is 20 most of the time, and decreases when array size is 1, 2 and 4 (to 8 bytes apart) and when array size is 8 (to 12 bytes apart). This may have something to do with the optimisation of memory read/write, though I cannot explain why such a large space is used for the array.
On the Linux server for int arrays of sizes from 0 - 10.
array size= 1, &arr = 0xff8a774f &x1 = 0xff8a773c, &y1 = 0xff8a7738, diff = 4
There seems to be no change in the 'distance' between addresses of x and y as array size increases. This may be because memory for the array is allocated before both the variables x and y (if the memory is allocated from 0xffffffff from 0x00000000).
4.
Repeat of 2: These results are obtained when there are no other global variables other than x and y.
on lab computer:
&x = 0x403030, &y = 0x403020, diff = 4 &x = 0x403030, &y = 0x403020, diff = 16
The difference between the address of x and y seems to change depending on whether there are other global variables at the beginning of the file. If there are, the difference increases (in one case to 8, 32), perhaps because the other global variables are allocated memory between x and y, thus increasing their address difference. Also, upon comparison with the data obtained previously, global and local variables are allocated a different part of memory, where local variables are given memory nearer the beginning, and global variables further on in memory.
on Linux server:
&x = 0x10010bb4, &y = 0x10010bb8, diff = -1 &x = 0x10010bb4, &y = 0x10010bb8, diff = -4
Similar effect seen on Linux server when other global variables are also declared.
Repeat of 3:
on lab computer:
array size= 0, &arr = 0x403050 &x1 = 0x403060, &y1 = 0x403020, diff = -16
No change upon changing array size from 0 to 10. Though I cannot explain why, since the array is allocated memory in between address x and y.
on Linux server:
array size= 0, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad0, diff = -4 array size= 1, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad4, diff = -8 array size= 2, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad4, diff = -8 array size= 3, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad4, diff = -8 array size= 4, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad4, diff = -8 array size= 5, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad8, diff = -12 array size= 6, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad8, diff = -12 array size= 7, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad8, diff = -12 array size= 8, &arr = 0x10010ad0 &x = 0x10010acc, &y = 0x10010ad8, diff = -12 array size= 9, &arr = 0x10010ad8 &x = 0x10010acc, &y = 0x10010ad0, diff = -4 array size= 10, &arr = 0x10010ad8 &x = 0x10010acc, &y = 0x10010ad0, diff = -4
Memory for global variables are given addresses very close to the beginning of memory in the Linux server (0x10010ad0).
There is also something unusual is occurring. For array sizes of 0 - 8, since memory for the array is allocated between that for x and y, as array size increases, so too does the distance between x and y. However, as soon as array size reaches 9, the address distance between x and y returns to 4 bytes. This is because when array size reaches 9, memory allocation for the array occurs after the allocation for x and y (though I don't know why). Therefore address distance decreases again.
5.
Using:
#include <stdio.h> int main(void){ int *p1, *p2; {int q; p1 = &q;} {int r; p2 = &r;} printf ("p1 = %p,\np2 = %p", p1, p2); return 0; }
As expected, pointers to memory addresses for local variables are similar to the ones seen earlier: p1 = 0x22cc9c, p2 = 0x22cc98
6.
Couldn't figure out how to resolve errors during compilation. Not done.
7.
on the lab computer:
&my_struct = 0xf91f19 offsets: my_char: 0 my_int: -2 my_long: -4 my_float: -8 my_double: -12
The memory for this array was allocated close to the end of memory. As expected, the addresses for each variable became further and further away from the beginning address for the structure. However, what was not expected was that the int variable only took up 2 bytes instead of the expected eight.
on the Linux server:
&my_struct = 0xff984720 offsets: my_char: 0 my_int: -2 my_long: -4 my_float: -8 my_double: -12
The results obtained from the linux server is identical to those obtained from the lab computer, other than the memory address. On the Linux server, the memory for the structure is also allocated near the end of memory, as in the lab computer.
8.
on the lab computer:
&my_union = 0x611021a0 offsets: my_char: 0 my_int: 0 my_long: 0 my_float: 0 my_double: 0
Memory for the union is allocated in a different part of memory - much close to the beginning than the structure. Also, the results obtained shows that there is no difference between the addresses of the union and of all the variables that are included in it.
on the Linux server:
&my_union = 0xfff0a748 offsets: my_char: 0 my_int: 0 my_long: 0 my_float: 0 my_double: 0
Results similar to those obtained from the lab computer. Unilke on the lab computer, memory allocated for the union is also near the end of memory, near the same region as for a structure.
9.
on the lab computer:
&sp1 = 0x22cca4 &sp2 = 0x22cca0 &sp3 = 0x22cc9c
It was fairly obvious that the memory that is freed would allow it to be allocated by malloc to other functions. So, it would not be surprising if &sp3 had the same address as &sp1 (though this did not happen in this case). If the pointer is used after the memory was used, the pointer may point to some unexpected data, as the old data has already been overwriten. Similar results are also obtained from the Linux server.
on the Linux server:
lab2h.c:6: warning: incompatible implicit declaration of built-in function 'malloc' &sp1 = 0xffeab75c &sp2 = 0xffeab758 &sp3 = 0xffeab754
10.
Used the following instead of code provided in the worksheet, due to an error upon compilation.
#include <stdio.h> int main(void){ printf("main = %p\n", main); return 0; }
on the lab computer:
main = 0x401050
on the Linux server:
main = 0x1000046c
This shows that for both the lab computer and for the Linux server, address for functions are given near the beginning of memory, at a similar location to global variables