5

I want to know, in c++, when does the initialization of objects take place?
Is it at the compile time or link time?
For ex:

//file1.cpp
extern int i;
int j=5;

//file2.cpp ( link with file1.cpp)
extern j;
int i=10;  

Now, what does compiler do : according to me, it allocates storage for variables.
Now I want to know :
does it also put initialization value in that storage or is it done at link time?

3 Answers 3

2

Actually there are different cases:

  • global variables or static variables (not classes): these values are stored in an init section of the exe/dll. These values are created by the linker based on the compiled object files information. (initialization on loading + mapping the dll/exe into memory)
  • local non static variables: these values are set by the compiler by putting these values on the stack (push/pop on x86) (compiler initialization)
  • objects: memory is reserved on the stack, the actual setting of values is deferred to the call to the constructor (run-time initialization)
  • pointers to objects (not actually a new case): space is reserved for the pointer only. The object pointed to exists only after a call to new that reserves memory and calls the constructor to initialize it (run-time initialization)
Sign up to request clarification or add additional context in comments.

4 Comments

What about "static const" members (not classes)? Do they really exist as a 'variable'? I think that every usage of them is simply replaced by the value and the "static const variable" just disappears. Right? For the rest, good overview, +1.
Ok..So in above code..will following things happen ? 1. while compiling file1.cpp, compiler leaves i as it is i.e doesn't allocate storage for i. 2. compiler allocates storage for j, but doesn't initialize it. 3. While compiling file2.cpp, compiler leaves j as it is i.e doesn't allocate storage for it. 4. compiler allocates storage for i, but doesn't initialize it. 5. While linking file1.o and file2.o, now let file2.o is initialized first, so now: Does j gets initial value of 0? or doesn't get initialized?
@Patrick, good point, I do not know for sure. I guess you are right, as the cost of having a variable is higher than an inlined value for basic types. However not 100% sure if all compilers behave that way...
@Happy Mittal, the linker doesn't care about the value while processing the object files. It only ensures an init section is created with the proper values for i and j. The order in which the .o files are processed does not matter. For the linker j is just the content of a specific address. At run-time j will be 5 and i 10.
0

As you said the compiler allocates storage for variables. I think the intialization value will also be done at compile time and not during link time.

3 Comments

But I read in a book that compiler can't determine what would be value in a storage, that's why we can't use something like this : int a=5; int arr[a];
@Happy If the compiler did enough static analysis to realize that that could be converted to int arr[5]; it would be fine, but they generally don't (it might not even be allowed by the spec)
The reason for int a=5; int arr[a]; is not defined is because the array size is undefined. Later you can change a to 10 that time how do you think the compiler will resizze the previously declared size.
0

There are no objects in your example, just ints. If by "initialization" you mean when are their values assigned, those ints will be converted to word-sized entries in a data section in the object file, which will be hard-coded with their initial values. The data section, along with the rest of the object file, is created by the compiler, so I suppose the answer to your question is compile-time

3 Comments

if u consider OOP then we can say int also as a class.
@vis Ints aren't classes in C++, they're a primitive type; there's no constructor call or any of the other scaffolding associated with classes
Thats correct. I understand. maybe we must change the question to Initialization of variables rather than objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.