1

In C if I simply declare a variable without initializing it, will compiler reserve space for it ?

  • If you don't even use it, it may be optimised out. In that case: no. Otherwise the compiler might reserve space for it *before* you define it. – Weather Vane Aug 10 '19 at 18:37
  • This is complicated because C evolved (was not completely planned from the start), so there are some complicated rules about which declarations are definitions based on where they appear, what keywords are used with them, and whether they are initialized. – Eric Postpischil Aug 10 '19 at 18:39
  • Read about the [as-if rule](https://en.cppreference.com/w/cpp/language/as_if). That applies to C too. – Deduplicator Aug 10 '19 at 18:43
  • If the declaration is a definition, you're guaranteed that space. Certain variable definitions are routinely optimized out such as unused locals or unused statics or locals and statics that are unused after other optimizations. Definitions of externally visible objects generally are not or cannot be optimized out. – Petr Skocik Aug 10 '19 at 18:45
  • We do not need to involve the “as-if” rule unduly here. If somebody asks “Does the `^` operator do exponentiation or XOR?”, we can simply answer that it performs an XOR—there is no need to say it might perform an XOR but the compiler could optimize it to something else or even remove it completely. That is a separate topic. I realize this question might have some concern about space used by the program, making the “as if” rule relevant. But jumping to “as if” is confusing; the rules **inside** the C model of computation need to be applied before behaviors outside the model are relevant. – Eric Postpischil Aug 10 '19 at 18:49

1 Answers1

-3

Not in the object or in the executable file, but at runtime the space required for the unitialized global & static data will be allocated in the application's memory.

EDIT: You can see these in the memory-map file of the executable, generated by the compiler (set the necessary options if a mapfile is not generated by default).



EDIT2: Well, because some people here were quick to downvote my answer, as if it had no merit at all, and wondering if I was actually wrong, I created a small test-project in Visual Studio 2017. I made three test-cases.

Case 1: Empty main() function

#include <stdio.h>

int main()
{
    return(0);
}

Case 2: main() function and uninitialized data

#include <stdio.h>

char g_data[16 * 1024];

extern char Ref_g_data();

int main()
{
    // Needed to actually include the data in the .exe, otherwise the linker will wipe them
    Ref_g_data();
    return(0);
}

This required writing a function in another source file, accessing the data:

extern char g_data[];

char Ref_g_data()
{
    return(g_data[0]);
}

Case 3: main() function and initialized data

Almost as above, just changed the g_data[] definition to initialized:

char g_data[16 * 1024] = "Hello";

And here are the results (filesizes):

Test    SrcMain.obj     TestCS.exe
Case 1  73,217 bytes    8,704 bytes
Case 2  73,307 bytes    8,704 bytes
Case 3  73,327 bytes    25,088 bytes

That is, the 16K data are indeed included in the executable if they are intialized.

Comparison of mapfiles:

Case 1:

DATA
 0002:000000f0 00000120H .rdata
DATA
 0003:00000000 00000018H .data

Case 2:

DATA
 0002:000000f0 00000120H .rdata
DATA
 0003:00000000 00000020H .data

Case 3:

DATA
 0002:000000f0 00004120H .rdata ---> 16K bigger
DATA
 0003:00000000 00000018H .data

I think the above prove that my answer was actually correct.

Constantine Georgiou
  • 2,412
  • 1
  • 13
  • 17
  • 2
    The correct answer is it depends on several factors. Some declarations that appear at file scope are not definitions and do not reserve space. – Eric Postpischil Aug 10 '19 at 18:40
  • The above proves what your implementation did in a few specific cases. It does not prove either what your implementation does in other cases or what the C standard requires. When you declare a variable with `extern` and without an initializer, it is not a definition, and the C standard does not require that storage be reserved for it. When you declare an object without `extern` and without an initializer at file scope, that declaration may or may not cause space to be reserved—it is only a tentative definition, and it might be that a different declaration provides the definition. – Eric Postpischil Aug 10 '19 at 23:02
  • Additionally, the statements about object files, executable files, and space in the application’s memory (properly process’s memory) cannot be made without qualification. These are outside the C model. It is common for uninitialized data not to occupy space in the object or executable file other than as information about how much space is needed and where it will be, but that is not universal. Some types of object or executable files might need to have byte-for-byte layouts so they are plain images that can be loaded by simple firmware. – Eric Postpischil Aug 10 '19 at 23:09
  • Or, if the data is not used in a particular execution of a program, it might not be loaded into memory at all—so the statement that the data will be allocated in the application’s memory is not correct. This question has been marked as a duplicate of a question with a [good answer](https://stackoverflow.com/a/45695028/298225). That shows you what should be said, at least first, about this question. – Eric Postpischil Aug 10 '19 at 23:10