SE250:lab-2:dsan039
Overview
The Lab invovled finding the size and location of pointers, variables, structures and unions. For me the most difficult part was learning (or trying) to use emacs, then sorting out the linux server to compile. While the results vary and I'm still unsure of whether they are correct or not, some are unexpected and represent areas that need to be addressed.
1
- all pointers are the same size (4 bytes)
- floats and integers are the same size also (4 byte)
2
&x = 0012FF48, &y = 0012FF4C, diff = -1 &x = 0012FF48, &y = 0012FF4C, diff = -4
when the types are cast to long before the calculation is done the difference is 4 (what is expected) vs. 1, which the c compiler is probably responsible for.
3
- sizeof reports the array as being 4 bytes.
&arr = 0012FF44 arr+4 = 1245000 &arr[4] = 0012FF48
- When changing the size of the array the addresses to the above changed, but not the addresses of x and y.
&x = 0012FF48, &y = 0012FF4C, diff = -1 &x = 0012FF48, &y = 0012FF4C, diff = -4
- The values of x and y did not change.
4
&x = 0040DA34, &y = 0040DA30, diff = 1 &x = 0040DA34, &y = 0040DA30, diff = 4 &arr = 0040DA38 arr+4 = 4250172 &arr[4] = 0040DA3C
- The addresses changed for all variables after having been made global. However, when the array was declared between x and y, then its length changed, the addresses of x and y did not change.
&x = 0040DA34, &y = 0040DA30, diff = 1 &x = 0040DA34, &y = 0040DA30, diff = 4 &arr = 0040DA38 arr+4 = 4250172 &arr[4] = 0040DA3C
- When the array size was set to 4 (which evidence at this point would suggest does nothing), and arr[4] set to 10, this did not change any of the results previously found (?) hopefully this can be explained in the next lecture or the maintenance report.
5
This code:
printf("p1 = %ld, p2 = %ld, %ld, %ld", p1, p2, &q, &r);
gives this output:
p1 = 1245000, p2 = 1244992, 1245000, 1244992
this all seems to be in order.
6
sp = 0012FF38(P ↕) sp XÆd = 0012FF38(P ↕) sp = 0012FF38(P ↕) sp XÆd = 0012FF38(P ↕) sp = 0040C0C0(tuvwxyz) sp XÆd = 0040C0C0(XXXXXXX) sp = 00891780(hijklmn) sp XÆd = 00891780(XXXXXXX)
- this is the output of the code (if I've implemented it correctly), although the compiler came up with a few warnings before running it. it looks strange and probably should be analysed in out next lecture.
7
&doom = 7E1C0AC7 offsets: my char: 0 my short: -2 my int: -4 my long: -8 my float: -12 my double: -16
this is the output for the structure definition, with the struct variable named doom. it looks pretty formulaic, only there is no offset between the base and the first char, which i think means they share the same address. what's strange about the following offsets is that it goes up in 2 steps of 2, then jumps to a difference of 4 between the integer and long integer. I'm unsure as to why this is.
8
&doom = 004030FC offsets: my char: 0 my short: 0 my int: 0 my long: 0 my float: 0 my double: 0
Well something here is strange, again i hope the lecturer can explain all this.
9
heres my code so you can see if i went wrong:
char *sp1, *sp2, *sp3; sp1 = malloc(10); sp2 = malloc(10); printf("&sp1 = %p, sp1 = %p\n", &sp1, sp1); printf("&sp2 = %p, sp2 = %p\n", &sp2, sp2); free(sp1); sp3 = malloc(10); printf("&sp3 = %p, sp3 = %p\n", &sp3, sp3); return 0;
I think that malloc is allocated a stack of memory which it climbs through, which is why the address of the pointer sp3 does not take the address sp1 once had. if this is correct then it is a property of the malloc function. However, the address of memory chunk sp1 was pointing to is given to sp3 once sp1 has been freed.