views:

555

answers:

7

Greetings.

I was give an assignment to basically explain thi. I have taken a quick look at the compiler documentation, and it seems to be a good place to start although it is quite extensive and I don't have much time. I'd like to know if I'd need to understand the C99 standards beforehand, or if there's another good source I can check. I'm going to be using it with Windows if it makes any difference. I also understand simple concepts such as heaps, stacks, linking and whatnot.

+2  A: 

AFAIK, g++ is simply a C/C++ compiler, nothing more. memory is managed according to the standard C/C++ libraries.

Javier
A: 

So, where could I find info about it? I couldn't find the documentation for the standards online.

+1  A: 

Any decent C/C++ tutorial should give the basics for this information - but memory management in C/C++ is a huge topic. Surely for an entry level class your instructor would give some guidance & probably a more specific, less open-ended question.

Michael Burr
A: 

He says we need to understand how objects, functions, variables, etc are put into the stack, what is allocated and when, etc. Not just that they go into the stack or the heap when dinamically allocated with new/pointers, but how/when/what order and how this impacts performance. He is pretty crazy.

You may wish to edit these updates into your original post. That makes it a lot easier to follow later on (Since answers are reordered based on rating)
jalf
A: 

Firstly, edit your question or comment, don't respond to your own question with follow ups (unless you have discovered the answer you were looking for and want to share it).

Anyway, you should read this:

http://www.gnomesane.net/code/doc/ptrarray/

It has a fantastic explanation about how dynamic memory is dealt with in c++.

Evan Teran
A: 

g++ is just a compiler. It follows the rules of the language it compiles (In G++'s case, C++, but you also mention C99).

And for your fairly specific questions, you may need to

  • Consult the language standard (For C++ this is ISO/IEC 14882 ). Unfortunately not free, but you can find drafts online for free that are basically as good as the real thing. The latest official version is C++2003 (ISO/IEC 14882-2003), but contains only very minor changes from the original '89. C++09 is getting close to completion too, and again, there are drafts available online for this. Be warned though, it is heavy reading, and I wouldn't recommend trying to find anything there unless you're already very familiar with the language.
  • Analyze the assembler code the compiler generates. The standard leaves a lot up to the implementation, so the only way to find out how G++ specifically pushes things onto the stack, in which order and so on, is to analyze the code it generates. (Also note that this likely changes between different versions of G++)

C++ is a notoriously underspecified language. There are huge chunks that just aren't covered by the standard, and where the compiler is free to do what it likes. This makes it a bit of a challenge to find out exactly what a given compiler is doing under the hood.

For this reason, you should also make sure you know exactly what you're expected to do. Dig up information on what the language says about memory management, or how g++ specifically deals with it?

jalf
'C++ is a notoriously underspecified language' -- I'd argue the opposite; many imperative languages are _over_ specified, killing performance in rare occasions. If you want a particular order, express that in code.
Aaron
You're right, but C++ is still underspecified. The two are not mutually exclusive. It's virtually impossible to write a large C++ program that doesn't depend on undefined behavior. There are too many parts of the standard that are unspecified.
jalf
A: 

I'm not clear on this question. If by "understanding memory management in mingw/g++" you mean "understand how the mingw g++ compiler handles memory internally while it is compiling files, such as when it allocates and frees abstract syntax tree nodes, etc.") then your answer is that as a multi-pass compiler GCC may not know the optimal lifetime for any particular piece of data but it does know that large groups of objects won't be needed from one pass to the other, so it uses memory pools when possible and garbage collection elsewhere.

On the other hand, if you're asking about how "how/when/what order ... objects, functions, variables, etc are put into the stack ... what is allocated and when and how this impacts performance" then you're in for a long night skimming through code.

Max Lybbert