Chris
Chris

Reputation: 683

Does a C compiler always, never, or sometimes exclude file-level arrays not touched by code?

We just encountered and solved an issue in which our RAM spiked when we included some code that accessed a certain large array. This leads to this follow on question: I apparently was under the misconception that C compilers excluded functions that weren't called, but didn't exclude arrays declared at the file level but weren't touched. I guess it makes all the sense in the world that it would do this, but I'm sure I've seen different behavior, just created an array and watched RAM usage jump (without writing code that touches the array). This was esp. shocking since we are at zero optimization.

So to learn the right lesson here: are arrays that are not touched always, never, or sometimes excluded by compilers. Does it depend on the compiler and the optimization level, or is this somehow tied to a C standard requirement? And am I crazy, or do most compilers seem to not exclude them?

Thanks.

Upvotes: 0

Views: 60

Answers (2)

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

You say "compiler" but this is a function of the linker: only the linker can know if an array is not used by any of the compilation units (object files).

The linker knows this, if no object file has a reference to the array (the data) that the linker has to resolve.

What remains is the compilation unit that declares the array (the data). That unit does not have (or does not need to have) a reference to the data because it is declared in the compilation unit (object file).

Taken together, there may be no way for the compiler or linker to know if some data is not used and consequently the linker will need to include it in the executable.


Note: if the array is declared static, then the compiler can decide because the data will have no visibility outside the current compilation unit.

Upvotes: 1

Lundin
Lundin

Reputation: 213989

As far as the C standard is concerned, C allows optimization, but a compiler need not optimize the code at all to be compliant.

As for how most systems work in practice, file scope variables are allocated in .data or .bss sections and have static storage duration, meaning that they have to get initialized to a value by the compiler before main() is called. This is a C standard requirement.

A compiler with optimizations disabled may therefore very well include such variables as part of the initialization code, regardless of if those variables are used or not. And most compilers have optimizations disabled as default.

You can help the compiler to do a better job at spotting unused variables by declaring them static - meaning that the variable gets "internal linkage" and no other file can touch it. If you don't, then the compiler might not be able to tell if the variable is used before compiling all other files, perhaps getting forced to leave that decision to the linker.

But overall, it isn't meaningful to ponder about what a compiler will do with optimizations off. If the unused variable is still allocated with optimizations enabled, that's when you should start to worry.

Upvotes: 2

Related Questions