CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Memory managment in C++

    Hi All,
    I am a newbie at C++ (Transferring from Java), and would like to understand some memory issue in C++.
    Say I have an array of objects, dynamically allocated using new[] operator. When I release it, I use delete[] operator.
    My question is how does the OS know the amount of memory to release? Or in other words, how does it keep track of the size of each array?
    Does it put the size of the array just behind its beginning?
    Does it keep a table somewhere telling for each address what size is allocated after it?

    Thanks,
    Guy

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Memory managment in C++

    Quote Originally Posted by guyafe
    My question is how does the OS know the amount of memory to release? Or in other words, how does it keep track of the size of each array?
    Does it put the size of the array just behind its beginning?
    Does it keep a table somewhere telling for each address what size is allocated after it?
    The method is implementation defined. Your ideas are plausible.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Memory managment in C++

    Thank,
    Can you please elaborate?
    Is it OS depended or compiler depended? (Or both)
    Do you know a good tutorial (or even a book) explaining this?

    Thanks,
    Guy

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: Memory managment in C++

    Quote Originally Posted by guyafe View Post
    Thank,
    Can you please elaborate?
    Is it OS depended or compiler depended? (Or both)
    Do you know a good tutorial (or even a book) explaining this?

    Thanks,
    Guy
    Both. The compiler has an implementation of memory management that ultimately relies on the OS, which has it's own implementation. In addition, you can overload new and delete, so you could also have your own implementation within your program.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Memory managment in C++

    Why do you need to know the details? You shouldn't try to tamper with it anyway.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Memory managment in C++

    Well' you'r right. I don't want to reinvent C++. Though I want to have some idea about how the machin works.

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Memory managment in C++

    How it's done in that particular C++ environment you use and how the OS (and also the hardware) does it vary as already said by others.

    Here's a schematic description of how the hardware do to virtualize the memory http://en.wikipedia.org/wiki/Memory_management_unit

    If you are very curious on how an OS and a compiler does things download the Linux kernel and gcc source.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Memory managment in C++

    Thanks,
    This was very helpful. Actually I'm starting to work on Linux kernel, so hopfully I'll understand it better.
    By the way, oddly I understand well how the MMU works (I have HW background). Its the virtual memory which I don't understand.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Memory managment in C++

    Quote Originally Posted by guyafe View Post
    Hi All,
    I am a newbie at C++ (Transferring from Java), and would like to understand some memory issue in C++.
    Say I have an array of objects, dynamically allocated using new[] operator. When I release it, I use delete[] operator.
    My question is how does the OS know the amount of memory to release?
    http://www.parashift.com/c++-faq-lit...html#faq-16.14
    Does it put the size of the array just behind its beginning?
    Maybe.
    Does it keep a table somewhere telling for each address what size is allocated after it?
    Maybe.

    You get the point.

    The C++ compiler can basically do anything internally to adhere to the ANSI/ISO C++ rules. That's what the "maybe's" are all about.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 1st, 2012 at 02:25 PM.

  10. #10
    Join Date
    Jul 2009
    Posts
    7

    Re: Memory managment in C++

    Quote Originally Posted by guyafe View Post
    Hi All,
    I am a newbie at C++ (Transferring from Java), and would like to understand some memory issue in C++.
    Say I have an array of objects, dynamically allocated using new[] operator. When I release it, I use delete[] operator.
    My question is how does the OS know the amount of memory to release? Or in other words, how does it keep track of the size of each array?
    Does it put the size of the array just behind its beginning?
    Does it keep a table somewhere telling for each address what size is allocated after it?

    Thanks,
    Guy
    http://www.parashift.com/c%2B%2B-faq...html#faq-16.14

    BTW, since you're coming of Java, it might be useful to know that in C++ (as opposed to C and, to some extent, Java or C#) you don't always have to manage resources manually -- you can use RAII to enable automatic resource(s) management (this is even more automatic than Garbage Collection (GC) in Java or C#, since GC only manages memory, not other resources like files, network sockets, GUI windows, etc.):
    http://www.hackcraft.net/raii/

    You might also want to take a look at "RAII is not Garbage" article here:
    [PDF] http://accu.org/var/uploads/journals/overload106.pdf

  11. #11
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Memory managment in C++

    Thanks.
    This link was very interesting. I spent the night reading all the FAQs in there :-)

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Memory managment in C++

    I suppose it's time to point out that in C++, the preferred way of making dynamic arrays is to use a std::vector, rather than to use new[] and delete[] directly.

  13. #13
    Join Date
    Jul 2009
    Posts
    7

    Re: Memory managment in C++

    Quote Originally Posted by Lindley View Post
    I suppose it's time to point out that in C++, the preferred way of making dynamic arrays is to use a std::vector, rather than to use new[] and delete[] directly.
    And, in general, RAII is preferred for resource(s) management in general and smart pointers for memory management in particular. I've already posted that as a reply, but apparently it got "moderated out" of the discussion, since I've linked to C++ FAQs, etc. Stands to reason, seemingly being too helpful is frowned upon... ;-)

  14. #14
    Join Date
    Jun 2009
    Location
    Israel
    Posts
    126

    Re: Memory managment in C++

    Quote Originally Posted by Lindley View Post
    I suppose it's time to point out that in C++, the preferred way of making dynamic arrays is to use a std::vector, rather than to use new[] and delete[] directly.
    You are right. The thing is that this exercise was specifically mentioned to use arrays instead of vectors.
    Also, I still wanted the understanding of how things work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured