-
January 1st, 2012, 01:06 AM
#1
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
-
January 1st, 2012, 01:26 AM
#2
Re: Memory managment in C++
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.
-
January 1st, 2012, 01:38 AM
#3
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
-
January 1st, 2012, 04:50 AM
#4
Re: Memory managment in C++
Originally Posted by guyafe
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.
-
January 1st, 2012, 07:51 AM
#5
Re: Memory managment in C++
Why do you need to know the details? You shouldn't try to tamper with it anyway.
-
January 1st, 2012, 08:00 AM
#6
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.
-
January 1st, 2012, 08:31 AM
#7
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.
-
January 1st, 2012, 12:03 PM
#8
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.
-
January 1st, 2012, 02:22 PM
#9
Re: Memory managment in C++
Originally Posted by guyafe
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.
-
January 1st, 2012, 02:22 PM
#10
Re: Memory managment in C++
Originally Posted by guyafe
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
-
January 2nd, 2012, 01:25 AM
#11
Re: Memory managment in C++
Thanks.
This link was very interesting. I spent the night reading all the FAQs in there :-)
-
January 2nd, 2012, 02:27 PM
#12
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.
-
January 2nd, 2012, 03:58 PM
#13
Re: Memory managment in C++
Originally Posted by Lindley
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... ;-)
-
January 3rd, 2012, 05:18 AM
#14
Re: Memory managment in C++
Originally Posted by Lindley
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|