hi,
another newbie question. Is being asked to draw memory layout of series of statemenst the same as being asked to write a trace program?
hi,
another newbie question. Is being asked to draw memory layout of series of statemenst the same as being asked to write a trace program?
I don't think so.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I hope the original question isn't formulated like that, it would be dreadful. The first question would be: how detailed must it be, what architecture, what compiler, etc.
Ok, well I'm only a first year college student so I don't think I'd be asked anything that complex. I'll find out more about it in labs. Thanks for the replies.
Sure, it's:
Draw the memory layout after each of the following statements:
char c = 'T', d = 'S';
char *p1 = &c;
char *p2 = &d;
char *p3;
p3 = &d;
p3 = p1;
*p1 = *p2;
Ahh its simple (if you know how pointers work?) , you need paper and a pencil and and for every char you will have to create box of 8 bits. For example, the following code
So in the above case i would create 2 box(representing memory location). One being for variable 'C' and other one for pointer '*p'. As I have declared two variables I need to boxes. And the third statement is I'm assigning pointer '*p' with the address of variable 'c'. As you know pointers can only point to memory locations. So in this case pointer '*p' is pointing at 'C'. So you draw an arrow from the box '*p' to the box c.Code:char c; char *p; p = &c
HINT: You require 5 boxes each representing char and char *, for your code shown.
Read this http://www.cs.cf.ac.uk/Dave/C/node10.htmlssharish
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
Thanking you!
Well, I have to say, the original question by whoever made up that question is extremely ignorant. There is no actual correct answer: first of all, who knows how much memory a pointer uses? And who says all these variables are stored in memory: I expect that with the tinyist bit of optimzation most of them will be stored in registers on most compilers anyway.
I think the question is "right" in the sense that it is what the instructor expects, but in reality there is no valid answer.
There certainly is a valid and correct answer here...
1) It is very easy to determine how much memory a pointer uses with sizeof()
2) The variables won't be stored in registers. Register storage ties up a CPU register and is extremely short term, generally lasting no more than the current function call.
3) The act of declaration creates memory slots for each variable and they are held there for the duration of the program. Again the size of these variables is discoverable with sizeof() and their location can be discovered using printf(" Addr: %X \n", &Varable)
Yes, we can use sizeof(). If the question would've been stated: "draw the memory layout of this program if it's ran on processor X when it is compiled with compiler Y (with flags A1...An).". Then we can use sizeof() to find out what the sizes are in this case. But there is no general answer for any architecture.
About point 2/3: that's wrong. Variables don't always allocate memory slots, it doesn't have to if it doesn't feel like it. In fact, I copied exactly this program into main (with nothing else), compiled it with -O3. The result:
That's right. Nothing at all. Not a single variable. Not a single action, even, except for (uselessly) creating the stack frame.Code:0x0000000100000f30 <main+0>: push %rbp 0x0000000100000f31 <main+1>: mov %rsp,%rbp 0x0000000100000f34 <main+4>: leaveq 0x0000000100000f35 <main+5>: retq
After this I added a print statement for the three pointers. The result:
Nope, not a single variable is allocated on the memory. Everything is, as I predicted, stored in registers.Code:0x0000000100000f00 <main+0>: push %rbp 0x0000000100000f01 <main+1>: mov %rsp,%rbp 0x0000000100000f04 <main+4>: mov $0x54,%ecx 0x0000000100000f09 <main+9>: mov $0x53,%edx 0x0000000100000f0e <main+14>: mov $0x54,%esi 0x0000000100000f13 <main+19>: lea 0x14(%rip),%rdi # 0x100000f2e 0x0000000100000f1a <main+26>: xor %eax,%eax 0x0000000100000f1c <main+28>: leaveq 0x0000000100000f1d <main+29>: jmpq 0x100000f28 <dyld_stub_printf>
So:
1. No, a variable does not need memory always. The fact gcc doesnt do this doesn't proof this, so if you still don't believe it I'll suggest you look it up in the standard.
2. These couple of variables can go into registers just fine. Well, at least after optimizing some away.