开发者

Allocation of memory ( C++ ) Compile-time/Run-time?

I am not sure how appropriate is this question, but -

I am curious about how the compiler sets memory aside for an object (allocation of memory) even before it is constructed (before even the constructor is calle开发者_如何学运维d!).

How does it happen for primitive datatypes?

This sounds a bit naive, but what exactly is it ?

Is it entirely a run time process, or does it (the compiler) have any plans like to do this, to do that, during run-time, which it decides before hand during the compile- time. I have no idea at all!

An object, be it a primitive type, a pointer, or a instance of a big class, occupies a certain known amount of memory. That memory must somehow be set aside for the object. In some circumstances, that set-aside memory is initialized. That initialization is what constructors do. They do not set aside (or allocate) the memory needed to store the object. That step is performed before the constructor is called.

In other words, when does the memory allocation for literally ANY kind of variable happen, in terms of time, at which point? At which step in compilation (or run-time)?


Memory allocation always happens at run time. Memory reservation, for objects that reside on the stack, or for static variables, happens at compile time (or at run time for C99 VLAs).

Memory for an object's members is always in place before the constructor runs. It is the job of the compiler and its runtime support to ensure that is so.


Allocation objects created with new or new[] or some variant is done at runtime, by accessing the freestore and finding enough space to place the new object, prior to the constructor running.

Allocation for local objects within a function are done at runtime. However, this is usually accomplished by moving a stack pointer the correct size of bytes, and the space between the previous value and the new value is now reserved for the object. The constructors are run after the space is run.

Allocation for global and static objects are done at compile time by the compiler, and their constructors are run when the translation unit they are defined in is loaded (usually before main() begins executing).

Allocation for objects contained directly (not via pointer) within another object is done as part of the allocation for that object.


There's (loosely speaking) three typical scenarios: allocation on the stack, allocation from heap, and static allocation.

The first is what happens whenever you declare a local variable within a function:

void foo ( )
{
   int bar = 42;
}

Here, the memory for bar is allocated on the stack. It is allocated at the time foo is called.

The second scenario happens when you create a class instance with the new operator:

void foo ( )
{
    MyClass* bar = new MyClass( );
}

Here, the memory for i is allocated on the heap. This again happens at runtime, and occurs as the new operator executes. It works essentially in the same manner a C's malloc, if you're more familiar with that.

Finally, there's static allocation.

void foo ( )
{
    static int bar = 42;
}

Here, the compiler knows ahead of time that memory will be needed for bar, and so it inserts into the executable an instruction telling the executable loader to reserve space, or literally makes a space in the executable for the variable to reside in. The memory for bar is therefore typically still allocated at runtime, as the executable loads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜