Thread: C++ memory questions

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    C++ memory questions

    I'm trying to clear up a little bit of my beginner's confusion about how things are stored in memory.

    Suppose I have the following code:
    Code:
    char buffer1[5];
    char buffer2[10];
    char *ptr1 = buffer1;
    char *ptr2 = buffer2;
    When I use gdb, I see that ptr1 contains an address ending in 07, and ptr2 contains an address ending in 1a. If I did my hex math correctly, that's 0x13 which is 19 bytes away. From this I gather that the stack is growing up. However, if buffer2 is sitting on top of buffer1 in the stack, and buffer2 is 10 bytes, shouldn't buffer2 be only 10 bytes away from the head of buffer1?

    Then I thought about memory only being word addressable, so really buffer1 takes up 8 bytes because that makes two words, and buffer3 takes up 3 bytes because that makes three words. Where does this "extra" storage get filled up? And even so, how can the blocks be 19 bytes away because that doesn't divide evenly into words.
    Last edited by jcafaro10; 04-05-2009 at 07:47 PM.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    5 bytes + 10 bytes + 4 bytes + 4 bytes. Maybe one of the pointers was located between one of the buffers. Sounds odd, but it might be the compiler trying to rearrange the stack frame in order to make exploiting more difficult. Either case, 19 can be acquired through both arrays and size of one of the pointers.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    What about word addressing?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Random memory questions
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 04-04-2007, 09:33 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM