Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the following memory layout, trace through the provided code and show the

ID: 3801531 • Letter: G

Question

Given the following memory layout, trace through the provided code and show the printf output (assume no errors in code). Note: long is a 32 bit word, addresses are in hex, and the location content is decimal.

int main() {

long *p=12;

long **p1=12;

long ***p2=12;

printf("%d ", *p);

printf("%d ", *(p + 1));

printf("%d ", *(p - 1));

printf("%d ", **p1);

printf("%d ", (***p2)-1);

printf("%d ", &p);

printf("%d ", *((&p)-1));

printf("%d ", *(int)(((char *)p)+8);

printf("%d ", *p++);

printf("%d ", *--p);

}

Addr Content 112 0x00 40 0x04 20 0x08 32 0x0C 0x10 20 0x14 48 0x18 16 0x1C 0x20 16 36 0x28 40 0x2C 0x30 0x34 P1 0x38 P2 0x3C

Explanation / Answer

32(value at the address of 12(0x0c in hexadecimal).In the above code we have assaigned address 12 to a pointer.so value in the address location ox0c from table is 32.

20(first increment the pointer address by 4 bytes as it is integer pointer .so base address is at ox0c from above step address of p and increment 4 bytes.so,address will become 0x10.The value at the address is 20.

20(similar to above step decrement address of p by 4 bytes.decrementing address by 1 means equivalent to address will decrease by 4 bytes.so final address will become 0x08.value at the address dereferencing the value is 20.)

4(pointer to pointer representation and value at the second level pointing.so keep it simple that first point the addrees location as 12 in decimal and it's addreess is 0x0c.we have another pointer it is pointing to the address location 32 in decimal but in hexadecimal is 0x20.The value at the 0x20 is required that is 4.)

39(Third level pointing so go with the address is 0x0c value in that is 32(0x20 hexadecimal)pointing to 0x20 value at the 0x20 is 4(0x04) again pointing to 0x04 is 40 minus 1 will get 39 as answer.

52 (address of the p is 0x34.The value of the address in decimal is 52

4(address of p 0x34 and decrement by 1 means 4 bytes so addreess will become 0x30 value at that address is 4)

48(pointer addreess is 0x0c and adding 8 bytes to it address will become 0x14 value in the address is 48)

32(pointer address is 0x0c value at the address is 32 and after it will oncrement value will modifiy to 33,but 32 will print.)

20(first decrement address and 0x0c decrement to the 0x08 and value at 0x08 is 20.)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote