Thread: memory layout??

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    memory layout??

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ungainly_one View Post
    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'd be interested in the original question...
    Can you post the exact text for me?
    Thanks.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    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;

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    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

    Code:
    char c;
    char *p;
    
    p = &c
    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.

    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

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Thanking you!

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ungainly_one View Post
    Thanking you!
    Harish beat me to it....

    But, please note for future that the way you present your question, has a direct bearing on the answers you will get.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by CommonTater View Post
    Harish beat me to it....

    But, please note for future that the way you present your question, has a direct bearing on the answers you will get.
    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.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by EVOEx View Post
    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)

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by CommonTater View Post
    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:
    Code:
    0x0000000100000f30 <main+0>:	push   %rbp
    0x0000000100000f31 <main+1>:	mov    %rsp,%rbp
    0x0000000100000f34 <main+4>:	leaveq 
    0x0000000100000f35 <main+5>:	retq
    That's right. Nothing at all. Not a single variable. Not a single action, even, except for (uselessly) creating the stack frame.

    After this I added a print statement for the three pointers. The result:
    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>
    Nope, not a single variable is allocated on the memory. Everything is, as I predicted, stored in registers.


    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.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    Harish beat me to it....

    But, please note for future that the way you present your question, has a direct bearing on the answers you will get.
    Apologies. I do appreciate you helping me out.

  14. #14
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by EVOEx View Post
    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.
    I think you're needlessly overcomplicating this, EVOEx. It's a simple question, with a simple answer, and you are expected to make reasonable assumptions.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ungainly_one View Post
    Apologies. I do appreciate you helping me out.
    No aplology needed.
    I was only dropping a (strong) hint.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  5. memory layout and declaration
    By cbastard in forum C Programming
    Replies: 6
    Last Post: 09-13-2005, 12:24 PM