SE250:lab-2:klod004

From Marks Wiki
Jump to navigation Jump to search

Lab 2 Purpose of this lab was to try to map the memory allocation of various variables. 1. Result of this task was that all pointers no matter what data type have the same size in bytes as they only store the addresses of the memory locations where the actual data is stored. 2. 2 variables of type int were declared and their positions in memory were printed out. The difference between both the addresses was also printed and found to be 3 on my computer. Upon observing other students, I noticed most of them got 1, so I just assumed there was some glitch with my compilation, but when I used (long)&x-(long)&y; this yielded a result of 12, which is a factor of 4 more than 3, so this somewhat calmed me down as most students had a factor of 4 as well. When using this notation, the difference is a direct arithmatic difference rather than a size difference, meaning, that it gives us the exact number of bytes that one variable is from another, rather than how many int sizes is it along in memory. 3. Insertion of an array between the 2 variables just inserted the array between the 2 variables with equal space on each side of the array. The sizeof command just returns the size of the array.The difference between the ints and the arr was given to be 24 when the size of the array was 4. 4. When the variables are declared globally, the array usually is located after both the int types, probably due to the compiler taking charge and trying to organize itself. And also when declared globally, the diff was given as 4. 5. The values of p1 and p1 store the the addresses of q and r. 6. The main thing noticed from this was that malloc and static used totally different ends of the memory. And the local, static, and malloc variables used different parts of the memory. 7. The structures were offset by: char = 0; short = -2; int = -4 long = -8; float = -12; double = -16; For union; All were 0; 9. It was found that the area in memory that was freed when used by malloc was then later reused. The area in memory is not freed until malloc frees it.