0

I have a question regarding dynamic initialization.

Example code

void main()
{
int a = 100;
//Statement1
//Statement2
...
float b = 6.32987; //StatementA
...
return;
}

The StatementA allocates memory at the middle execution of program. How does process takes place and where does space gets allocated for variable b (stack or heap)?

  • 3
    This is heavily language dependant. Please state your language. – Euphoric Jan 13 '14 at 8:30
2

C is not an interpreted language.

A typical C compiler will analyze the source code, during this analysis it will find all the automatic variables allocated in a function and assign them a suitable place on the stack.

In your example the variable is assigned an initial value. The compiler will generate the code to initialize all such variables every time the function is entered, this will typically take place before any of your code is executed.

The position of the definition in the source code has no effect on the code the compiler generates to reserve storage for and initialize the variable.

Also note that for many compilers using aggressive optimization many of the above steps will be optimized out of existence. For instance all initialized variables may be gathered together in a block and a single block copy used to initialize them with a single instruction.

  • Hey thankx for replying here I am not worried about C Language. Older version's of C does not allow these kind of dynamic initialization. The language is C++ and Java. Does the variable "b" gets stored in stack or heap? – Rizwan S A Jan 13 '14 at 12:42
  • @RizwanSA: The question was tagged "C", so it's assumed the question was about C. If it's really about C++ and Java, it needs to be tagged with those languages. In C++, b has auto storage, meaning it will be allocated in exactly the same manner as a (whether that's on a hardware stack or not is up to the implementation). In Java, both objects are primitive types, so they aren't allocated from the heap. – John Bode Jan 13 '14 at 17:27
  • @Rizwan -- the answer applies to nearly all compiled languages. The compiler works out all the storage required, assigns an address to each piece referenced before it generates the machine instructions. – James Anderson Jan 14 '14 at 1:49
0

You tagged this as C, but in another comment mention C++ and Java. I'll try to cover all of them.

C:

From the latest online C standard:

6.2.4 Storage durations of objects

...

5 An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration, as do some compound literals. The result of attempting to indirectly access an object with automatic storage duration from a thread other than the one with which the object is associated is implementation-defined.

6 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.

Neither a nor b are declared static, nor are they declared with any explicit linkage, so they have auto storage duration; they only exist for the lifetime of their enclosing scope (logically speaking). In practice, the storage for both a and b will (usually) come from the runtime stack, which will (usually) be set aside at function entry and (usually) released at function exit. However, that's an implementation detail; as long as the behavior conforms to the standard, the exact nature of the storage (stack, heap, or otherwise) doesn't really matter.

As far as storage is concerned, it doesn't matter when b is declared within the function; after the source code has been translated to machine code, storage will be set aside at function entry.

C++:

From the online C++ standard:

3.7.2 Automatic storage duration [basic.stc.auto]

1 Local objects explicitly declared auto or register or not explicitly declared static or extern have automatic storage duration. The storage for these objects lasts until the block in which they are created exits.

2 [Note: these objects are initialized and destroyed as described in 6.7. — end note ]

3 If a named automatic object has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to be unused, except that a class object or its copy may be eliminated as specified in 12.8.

For primitive types, the behavior is almost identical to C; storage is (usually) allocated from the runtime stack at function entry, and (usually) released at function exit. For instances of class types, it gets a little more complicated.

Java:

From Section 2.6 of the Java Virtual Machine Specification:

2.6. Frames

...

A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception). Frames are allocated from the Java Virtual Machine stack (§2.5.2) of the thread creating the frame. Each frame has its own array of local variables (§2.6.1), its own operand stack (§2.6.2), and a reference to the run-time constant pool (§2.5.5) of the class of the current method.

...

2.6.1. Local Variables

Each frame (§2.6) contains an array of variables known as its local variables. The length of the local variable array of a frame is determined at compile-time and supplied in the binary representation of a class or interface along with the code for the method associated with the frame (§4.7.3).

...

In all three cases, the behavior will be the same (allowing for differences in how main is defined; the posted snippet isn't 100% correct for any of C, C++, or Java).

  • So this means memory for float variable "b" is allocated statically in stack and this happens at start of main function. – Rizwan S A Jan 14 '14 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.