SE250:lab-2:jsmi233
Task 1
All pointers are the same size on a given machine irrespective of what they point to. On the lab machines the size of a pointer is the same size as an int and a long, that is, four bytes. My results for the linux server are the same.
Task 2
int x, y; printf("&x = %p, &y = %p\n", &x, &y); printf("(long)(&x - &y) = %d\n", (long)(&x - &y)); printf("(long)&x - (long)&y = %d\n", (long)&x - (long)&y);
(long)(&x - &y) is 1 and (long)&x - (long)&y is 4 on both machines. The reason one result is 1 and the other result is 4 is because of the memory illusion that c uses. (long)(&x - &y) returns the number of integer sized spaces between the starting points of x and y in memory.
A note about local/auto variables: On the lab machine &x = 0x22cca4, this seems to be the location of the first local variable in all the programs I have run today. On the linux server, &x = 0xffe74728.
Task 3
The size of the char array with four elements, ie sizeof(arr), is four. The value of arr+4 is equal to the value of &arr[4] and four greater than the value of arr which is equal to the value of &arr. These results are the same on both machines.
x = y = 0; arr[4] = 10; printf("x = %d, y = %d\n", x, y);
The result of this code on the lab machine was x = 10, y = 0. This is understandable since I was accessing the array out of bounds. However the result on the linux server was quite unexpected. It output x = 167772160, y = 0. But maybe a more erratic result should have been expected since arr[4] and x are different types.
int x; char arr[ARRAY_SIZE]; int y; printf("(long)&x - (long)&y = %d\n", (long)&x - (long)&y);
Here are the results as I varied ARRAY_SIZE between 0 and 10:
Size Local Global Linux Linux Global
0 32 32 4 4
1 8 32 4 4
2 8 32 4 4
3 32 32 4 4
4 8 32 8 4
5 32 32 4 4
6 32 32 4 4
7 32 32 4 4
8 16 32 4 4
9 32 32 4 4
10 32 32 4 4
These results seem quite erratic for the lab machine when the variables are declared erratically. Why?? The linux server seems to be ignoring the order in which the variables appear in the code. Although for some reason when ARRAY_SIZE = 4, the linux local variables x, y have a gap of 8 rather than 4. Why??
Task 4
I repeated task 2 with global x and y. The results were the same for the linux server as when the variables were declared locally, but for the lab machine the results were fourfold. Maybe the lab machine stores extra information about the variables in the static part of memory. Why wouldn’t the linux server do the same thing??
Task 5
int *p1, *p2; {int q; p1 = &q;} {int r; p2 = &r;} printf("&p1 = %p, &p2 = %p, p1 = %p, p2 = %p\n", &p1, &p2, p1, p2);
Lab machine: &p1 = 0x22cca4, &p2 = 0x22cc9c, p1 = 0x22cc9c, p2 = 0x22cc98
Linux server: &p1 = 0xffe6472c, &p2 = 0xffe64728, p1 = 0xffe64724, p2 = 0xffe64724
The difference between the behaviour on the two machines is this (if I am allowed to guess): The linux server forgets about “int q” as soon as it goes out of scope and so “int r” is allocated in the same space (since p1 == p2). However the lab machine seems to wait until the end of the function before “forgetting” about either variable.
Task 6
Lab machine:
sp = 0x22cc70(456)
sp X’d = 0x22cc70(-0@)
sp = 0x22cc70(fg)
sp X’d = 0x22cc70(-0@)
sp = 0x402000(tuvwxyz)
sp X’d = 0x402000(XXXXXXX)
sp = 0xfc2150(hijklmn)
sp X’d = 0xfc2150(XXXXXXX)
Linux server:
sp = 0xffdf36f4(0123456)
sp X'd = 0xffdf36f4(XXXXXXX)
sp = 0xffdf36f4(acdefg)
sp X'd = 0xffdf36f4(XXXXXXX)
sp = 0x10010de0(tuvwxyz)
sp X'd = 0x10010de0(XXXXXXX)
sp = 0x10011008(hijklmn)
sp X'd = 0x10011008(XXXXXXX)
The lab machine fails when dealing with pointers to expired local variables, but the linux server manages admirably. Why??
Task 7
The struct has the following offsets on both machines:
My_char 0
My_short 2
My_int 4
My_long 8
My_float 12
My_double 16
As was pointed out during a class meeting, it is interesting to note that the char seems to take up 2 bytes.
The offsets are all zero for the union. This makes sense and should be expected.
Task 9
One thing I noted about the malloc function is that it does not return the same pattern of addresses every time the program is run.
On the lab machine, sp3 is assigned the memory previously allocated to sp1, and I guess using sp1 would conflict with the use of sp3. The same is the case for the linux server.
On one occasion, the lab machine gave me a memory allocation at 0xfa2148, and the linux server gave me a memory allocation at 0x10011008.
Task 10
I printed the address of the function local_str
Linux server: 0x100004dc
Lab machine: 0x401050
These were the same each time I ran the program.