Does the compiler do anything during runtime?

I'm reading C++ Primer 5th Edition by Lippman. In the chapter leading up to the topic of smart pointers, this was said about static and automatic variables:

Static memory is used for local static objects, for class static data members, and for variables defined outside any function. Stack memory is used for nonstatic objects defined inside functions. Objects allocated in static or stack memory are automatically created and destroyed by the compiler.


I thought the compiler's job was done once an executable file is produced. It isn't something that runs concurrently with a program to "create and destroy" static objects right?
Last edited on
Yes once the program is compiled the compiler is finished.

Objects allocated in static or stack memory are automatically created and destroyed by the compiler.


This means that the compiler produces code to create and release all of the memory used (except manually allocated dynamic memory) by the program as part of compilation.

Nonetheless, each compiler has a "runtime library" (not to be confused with the C/C++ Standard Library) that contains compiler-specific code that will be linked into the produced EXE (or DLL) file and that will be executed at runtime in order to provide certain services. For example, for certain operations the compiler won't generate the code "in-place" but will instead issue a call to its associated runtime library.

So, while the compiler itself doesn't do anything at runtime, the compiler's associated runtime library does.
Last edited on
There are many incorrect facts related to this floating around. I have read/heard many people claim local (static or non-static) variables are allocated/created at compile time. I think part of this is just imprecise language (it's easier to say "the compiler did X" than to say "the compiler generated code that made X happen at runtime" ) but a lot of it is probably down to misunderstanding or poor understanding of how things work and what it actually means for something to happen at compile time vs. runtime.
Nonetheless, each compiler has a "runtime library"


Usually referred to as CRT (c runtime). This is often compiler/linker specific and .exe files produced require the right version to be available.

FYI, ElusiveTau, you are reversing the order of your tags, ending them before you use them.
The answer is no .. neither the compiler nor the linker do anything at run time. C++ programs are stand alone executable code that works on machines that do not have the compiler or linker installed. The libraries injected into the program must be there, and in that sense the CRT is no different from any other library like gzip ... the CRT is usually on the machine but not always: any windows user knows what its like to be installing a program that decides it needs to install the run-time in the middle of the process, that is what is going on when that happens, its putting that CRT on the box.
It might be helpful to review the process of compiling C++ code into an executable.

https://riptutorial.com/cplusplus/example/26378/the-cplusplus-compilation-process
Topic archived. No new replies allowed.