Dynamic initialization, how does it work?

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)?

1

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.

3

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).

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật