memory allocation

This is a discussion on memory allocation within the C Programming forums, part of the General Programming Boards category; I might not be fully understanding what's going on here, but when I allocate two segments of memory for two ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    69

    memory allocation

    I might not be fully understanding what's going on here, but when I allocate two segments of memory for two variables, the address difference between the two doesn't seem to account for the amount of space that I'm allocating.

    Code:
    int                                                                          
    main(int argc, char* argv[]) {                                               
                                                                                 
      int *foo = malloc(4*sizeof(int));                                          
      int *bar = malloc(sizeof(int));                                            
                                                                                                                                          
      printf("foo: %X\n", (unsigned int) foo);                                
      printf("bar: %X\n", (unsigned int) bar);                                        
                                                                
      return 0;                                                                  
    
    }
    Output:

    Code:
    foo: 100120
    bar: 100130
    Given that I'm allocating 4 x sizeof(int) for foo (16 bytes), and assuming that allocation is contiguous, I'm expecting there to be a separation of at least 16 between the output for foo and bar (i.e. 100120 and 100136). What am I missing? Thanks in advance!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The difference between 100136 and 100120 is twenty-two bytes. The difference between 100130 and 100120 is sixteen bytes. (Look at your format specifier.)

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by tabstop View Post
    The difference between 100136 and 100120 is twenty-two bytes. The difference between 100130 and 100120 is sixteen bytes. (Look at your format specifier.)
    Thanks for the tip on the format specifier. Changing it to %d gave me the information that I had expected to see.

    I do have another question, though. How did you come to the conclusion that there were sixteen bytes between 100130 and 100120 (apologies for being dense).

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Who says that the two blocks must be right next to each other? Theoretically, malloc() can allocate anywhere from the heap, giving the appearance of a huge gap between memory.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    It's always easy to make such a mistake. Your two Hexadecimal values converted to decimal differ by 16.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's why I said look at your format specifier: %x means "print this unsigned integer as a hexadecimal integer." (That's also why you got a warning about passing a pointer as an integer without a cast.) The format specifier for pointers is %p, and is system-specific (but is also often hexadecimal).

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by MacGyver View Post
    Who says that the two blocks must be right next to each other? Theoretically, malloc() can allocate anywhere from the heap, giving the appearance of a huge gap between memory.
    Yes, I understand that. But in this case, the gap seemed pretty close, and I was trying to understand why I wasn't seeing 16 bytes in between the two numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 06:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 04:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21