C++ Variable Memory Allocation

These are mostly compiler design questions. When your compiler compiles this, for example:

int * pData = new int[256];

How is the memory allocated on-the-fly? Does the compiler make a call to an OS routine that allocates memory for you or is a function compiled that allocates the memory for you?

Also, when you write something like this:

if (x == 5)
    int y;

Since the memory is not allocated at runtime, I'm assuming that the data takes up some room in the data segment of the program. Since the compiler can't really determine whether or not the int y; branch will be executed at runtime, is the memory reserved for the variable whether or not int y; is executed? And if it's reserved regardless, isn't it more memory-efficient to mem alloc any variables that are in a block that may or may not be executed?

oo thanks


For the first question: When the new operator is encounter by the compiler like in your example:

int * pData = new int[256];

It effectively emits code which looks like this:

int *pData = reinterpret_cast<int*>(::operator new(256 * sizeof(int)));
// the compiler may also choose to reserve extra space here or elsewhere to
// "remember" how many elements were allocated by this new[] so delete[] 
// can properly call all the destructors too!

If a constructor should be called, that is emitted as well (in this example, no constructor is called I believe).

operator new(std::size_t) is a function which is implemented by the standard library, often, but not always , it will boil down to a malloc call.

malloc will have to make a system call, to request memory from the OS. Since OS allocators usually work with larger fixed sized blocks of memory, malloc will not have make this call every time, only when it has exhausted the memory it currently has.


For the second question : For local variables, it really is up to the compiler. The standard makes no mention of a stack. However, most likely you are on a common architecture running a common OS and using a common compiler :-).

So for the common case, the compiler will typically reserve space at the beginning of the function call for all local variables by adjusting the stack accordingly or reserving registers for them if it can (a register being the preferred choice since it is much faster).

Then (in c++), when the variable is encountered, it will call the constructor. It could in theory, adjust the stack on an as needed basis, but this would be complicated to prove correct and less efficient. Typically reserving stack space is a single instruction, so doing it all at once is pretty optimal.


How is the memory allocated on-the-fly? Does the compiler make a call to an OS routine that allocates memory for you or is a function compiled that allocates the memory for you?

In most implementations, it's a bit of a combination of those two. There is normally an operating system service that is used to provide blocks of memory to your process, and also some code in the runtime or the standard library that manages those blocks of memory on a finer-grained scale inside your application.

is the memory reserved for the variable whether or not "int y;" is executed? And if it's reserved regardless, isn't it more memory-efficient to mem alloc any variables that are in a block that may or may not be executed?

Normally, no - the space for y would be allocated only if the appropriate code were executed. Variables with automatic storage class are normally on the stack, so making space for them is as easy as modifying the stack pointer. That said, the compiler can do pretty much whatever it wants in this case, so inspecting the output from your toolchain is the only way to know for sure.


Local variables (int y) are allocated on the stack.

The same thing goes for the pointer, however the 'new'-ed expression is allocated on the heap, usually through a malloc call of sorts. The exact method and heap layout are implementation-defined.

Only global static data are in the data-segment.

The example int y will be optimized out because it isn't used.

Unlike eg C#, C++ is not allowed to reduce the scope for a variable so wrapping a shortlived local in { SomeClass y; } could help, so it gets destructed earlier

However, I'm pretty sure that for simple types (like int) no such restriction exists, because there can never be a destructor for those

链接地址: http://www.djcxy.com/p/78530.html

上一篇: 对C ++动态内存分配的确切含义有点困惑

下一篇: C ++可变内存分配