Search code examples
cmemory-managementcompile-time

data structures allocated at compile time?


I guess I have a bunch of stupid questions that have been eating my head up and I couldn't really find satisfying answers anywhere, so might as well ask here and get looked down upon than keep it floating inside my head. I'll get straight to it.

  1. If I have some variables and arrays (whose size is known at compile time), inside a function, is it still allocated if the function is not called at all?

  2. How does this relate to the size of the .c file and the .out executable?

  3. Where can I find stuff like these? You know, some good programming books that not only teach the language, but also the underlying working principles in memory.


Solution

    1. If I have some variables and arrays (whose size is known at compile time), inside a function, is it still allocated if the function is not called at all?

    What the spec says

    If you declare static objects inside a function then they exist and retain their values for the entire lifetime of the program. There is only one copy of each such variable, regardless of how many times the function is called, and regardless even of concurrent and recursive calls.

    If you declare non-static objects inside a function then each executaion of the innermost containing block gets its own copy of each one, and each such copy ceases to exist when the block execution with which it is associated terminates.

    What you may see in practice

    C implementations are allowed in many respects to deviate from the details of the specification as long as they produce the same observable behavior as if they had followed the spec to the letter. Among the variances you might see are

    • If the compiler determines that a given function cannot ever be called, then the whole function may be omitted, along with any variables it declares.

    • If the compiler determines that a given variable cannot ever read then then the variable and any writes to it might be omitted altogether (unless it's volatile).

    • In principle, a program might not allocate or initialize static variables until they are first accessed (though this is unlikely).

    • Tail-recursive function calls may be collapsed into iterations, such that the function's local variables are re-used instead of new ones being created for each recursive call.

    1. How does this relate to the size of the .c file and the .out executable?

    The .c source file is made up of the code you write. If you write more code then it is larger. If you write less code then it is smaller.

    Perhaps you meant object files (.o by Unix convention). The spec does not speak to the question here, or even speak directly to such files at all. In practice, implementations vary, but it is common that of all local variables retained by the compiler, only those that are static and declared with nonzero initializers take up space in object files, binary libraries, or executable files.

    1. Where can I find stuff like these? You know, some good programming books that not only teach the language, but also the underlying working principles in memory.

    I'm sorry, but this part of the question is off-topic for Stack Overflow. We do not address questions soliciting recommendations for off-site materials, including, but not limited to, textbooks and tutorials.