Paul Manta
Paul Manta

Reputation: 31577

Can compilers change the order of declarations?

In a declaration such as int i, v[5], j;, how will the variables be allocated? Is the compiler allowed to change their order?

Upvotes: 1

Views: 110

Answers (3)

Robin Summerhill
Robin Summerhill

Reputation: 13675

Yes, the compiler can (and will) change the order. Ordering is compiler-specific and not specified in the C standards. The C standards don't even specify that a stack should exist.

Upvotes: 2

nos
nos

Reputation: 229128

Yes, the compiler can do whatever it wants, as long as the meaning of the program stays the same. These variables might be optimized out of existence, stored only in a register, reused for other purposes, reordered for alignment requirments.

(note that a compiler cannot reorder variables within a struct)

Upvotes: 3

K-ballo
K-ballo

Reputation: 81349

The compiler is allowed to allocate them pretty much wherever it wants.

Upvotes: 1

Related Questions