SE250:lab-2:twon069
Task 1
My codes
int main(void){ int intVariable; double doubleVariable; float floatVariable; short shortVariable; long longVariable; char charVariable; printf("int = %d\ndouble = %d\nfloat = %d\nshort = %d\nlong = %d\nchar = %d\n\n", sizeof(intVariable), sizeof(doubleVariable), sizeof(floatVariable), sizeof(shortVariable), sizeof(longVariable), sizeof(charVariable)); printf("&int = %d\n&double = %d\n&float = %d\n&short = %d\n&long = %d\n&char = %d\n\n", sizeof(&intVariable), sizeof(&doubleVariable), sizeof(&floatVariable), sizeof(&shortVariable), sizeof(&longVariable), sizeof(&charVariable)); return 0; }
The result were that all addresses have the same size of 4 byte while the actual data type were different sizes. "int", "float" and "long" all have the same size as the addresses. The same apply under Linux
There was no difficulty with this task beside fiddling with emacs and the Linux server to get them working.
Task 2
My codes
#include <stdio.h> int main(void){ int x; int y; printf("&x = %p, &y =es %p, diff = %ld(long - long = %ld)\n", &x, &y, (long)(&x - &y), (long)&x - (long)&y); return 0; }
The result was that the address difference is 1 when the address were not converted to long before the subtraction thus the subtraction was the difference in address of the value ie. 1, because the address were next to each other. However when the address were forced to convert to long before the subtraction, the result were 4, which means the result was a size difference of the two address, which is of course 4 bytes as shown in Task 1. The same apply to Linux, though the address was different.
Task 3
Part A
My codes
int main(void){ int x; char arr[4]; int y; printf("&x = %p, &y =es %p, diff = %ld(%ld)\n\n", &x, &y, (long)(&x - &y), (long)&x - (long)&y); printf("&arr = %p\n\nsize of arr = %d\n\narr+4 = %p\n\n$arr[4] = %p\n\n", &arr, sizeof(arr), arr+4, &arr[4]); return 0; }
- The size of the array is 4 (obviously).
- arr+4 adds 4 bytes to the address of arr, which returns a value the same as &x (as the size of x is 4 bytes, when arr adds 4, it moves 4 bytes "back" to x's address).
- &arr[4] and arr+4 are treated the same, moving 4bytes past the start of the array.
Part B
When changing arr[x] between 0-10, result are rather strange:
In Window(emacs):
x = 0, dif = 32 bytes x = 1, dif = 8 x = 2, dif = 8 x = 3, dif = 32 x = 4, dif = 8 x = 5, dif = 32 x = 6, dif = 32 x = 7, dif = 32 x = 8, dif = 16 x = 9, dif = 32 x = 10,dif = 32
In Linux:
x = 0, dif = 4 bytes x = 1, dif = 4 x = 2, dif = 4 x = 3, dif = 4 x = 4, dif = 8 x = 5, dif = 4 x = 6, dif = 4 x = 7, dif = 4 x = 8, dif = 4 x = 9, dif = 4 x = 10,dif = 4
Upon inspecting on when x = 4, I realise Linux only place the array between x and y when x = 4, and not any other size between 0-10, rather strange. Window always places it between x and y, however the size seen to vary differently, weird.
Part C
After assigning x = 0, y = 0, arr[4] = 10, when printing value x, it becomes 10, because arr[4]'s address points directly at x (theres only arr[0->3]). However Linux gives a weird result, weird....
Task 4
- only done in windows, need to go uni to complete rest on linux
Part A
When x and y are placed as global variables, the following result are seen:
&x = 0x403010, &y = 0x403014, diff = -1(-4)
- This shows the 2 variables are now placed in a totally different location as to where it was initially placed as a local variable.
- Also the 2 variable's order of placement are reversed which place x in a lower address and y in a larger address.
Part B
- Due to x and y being placed in a totally different memory location the change of size of arr does not affect anything.
Task 5
Codes:
int main(void){ int *p1, *p2; printf("p1 = %p, p2 = %p\n", p1, p2); {int q; p1 = &q;} {int r; p2 = &r;} printf("p1 = %p, p2 = %p", p1, p2); return 0; }
Result:
p1 = 0x401390, p2 = 0x611021a0 p1 = 0x22ccdc, p2 = 0x22ccd8
- values of p1 and p2 are the address of int q and r respectively.
Task 6
Part A
The output are:
sp = 0x22ccb0(456) sp = 0x22ccb0(efg) sp = 0x402000(tuvwxyz) sp = 0x6c0158(hijklmn)
- some values seen to be lost when the the pointer is returning a local variable such as char s[8] = "0123456".
- use of static and malloc retains the full string of variable as they're fixed when run.
- malloc and statis also uses different memory location.
Part B
The output are:
sp = 0x22ccb0(456) sp X'd = 0x22ccb0(-0@) sp = 0x22ccb0(efg) sp X'd = 0x22ccb0(-0@) sp = 0x402000(tuvwxyz) sp X'd = 0x402000(XXXXXXX) sp = 0x6c0158(hijklmn) sp X'd = 0x6c0158(XXXXXXX)
- the local variable can't seen to be found when sp being declared with Xs.
Task 7
Result of Task 7:
&my_struct = 0x0 offsets: my_char:0 my_short:-2 my_int:-4 my_long:-8 my_float:-12 my_double:-16
- the result shown displays both the sequence of each variable and also their size
- the number decrease due to the variables placed are in increasing orders in the memory
- each variable size can be shown by subtracting the negated number of the next variable by the negated number of the variable, eg short's size can be calculated by taking int's 4 and subtracting by short's 2, showing it takes a size of 2 in memory
Task 8
Result of Task 8:
&my_union = 0x0 offsets: my_char:0 my_short:0 my_int:0 my_long:0 my_float:0 my_double:0
- the result seen to show that union's variable either have no memory preallocated or all variables' memory space are as big as the union itself
- upon searching on google about union in c, i realise union works the same way as struct, however all values are allocated the same memory location with the size of the biggest variable within the union itself. What this means is that if we are to store a float variable in the union's my_float, and retrieve the my_int variable from the same union, we will result in a weird value which is actually the value of my_float. The reason for union is when memory space is to be conserved, however the coder is required to keep track of the variable stored in the union.
- the above explanation can be found here
Task 9
Output:
*sp1 = 0x6b0150 *sp2 = 0x6b0160 *sp3 = 0x6b0150
- the pointers returned by malloc is a part of memory that is unused
- by freeing the pointer sp1, then assigning sp3 results in sp3 using the freed memory from sp1 (provided that it is the same size)
Task 10
- Just a side note, I'm writing the code in TextPad, and realise it saves every changes done on the current task, lol, was annoyed when realising I needed Task 6 codes again and I just deleted it all before... but got it back with a very long ctrl+z :P, god bless textpad xD
Result:
local_str = 0x401050 sp = 0x22ccb0(456) sp X'd = 0x22ccb0(=0@) sp = 0x22ccb0(efg) sp X'd = 0x22ccb0(=0@) sp = 0x402000(tuvwxyz) sp X'd = 0x402000(XXXXXXX) sp = 0x6c0158(hijklmn) sp X'd = 0x6c0158(XXXXXXX)
- function seen to live close to the static memory location (function is a constant??:S)