3

Take this example.

#include "stdio.h"

int global_var=5;

int main ()
{
    int local_var=6;
    //some statements

    return 0; 
}

If the main function is the only entry point, then when does the declaration and assignment of global_var happen?

On a related note, is the global_var allocated in the heap or the stack? Also, is there a way to declare a global variable from a function, while respecting the entry point?

kgbook
  • 388
  • 4
  • 16
Della
  • 1,264
  • 2
  • 15
  • 32
  • Sorry, bad typo. Corrected it. – Della Aug 16 '16 at 07:02
  • The title says C, the tags also mention C++, and the example code is valid in both languages but that doesn't mean the answers are the same for both languages. In particular, C++ has constructors for global objects. – MSalters Aug 16 '16 at 09:29

2 Answers2

7

Conceptually, the initialisation of global variables happens before main is entered. Here I'm assuming that all your code was compiled in one translation unit: more formally a global variable is initialised immediately before any function defined within the translation unit defining that global variable is encountered. (Although a compiler can optimise this if there are no side effects).

Neither C nor C++ mention the heap or stack in their standards: they are implementation concepts, not language concepts.

So global_var could be allocated on a heap, but it might be on some kind of stack that is set up before main is entered.

There's no way of declaring a global variable within a function. A static variable within a function can mimic much of the behaviour of a global varaible but conceptually, the static is initialised the first time the function is encountered.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thanks. I moved the declaration of global_var after the main function. Then the main function fails to recognise it. So apparently, in that case, the compiler respects the main as the entry point. – Della Aug 16 '16 at 07:12
  • 1
    @Roy You're mixing up compilation and runtime. If you add a declaration for the global var before `main`, you can have its definition after `main` and all will work. – Angew is no longer proud of SO Aug 16 '16 at 07:13
  • Are you sure? IIRC, initialisation of global variables happens before the first function in the Translation Unit is called, which _may_ be after `main` is entered. This can happen even in the presence of side effects. This stops a few tricks that would rely on classes to register themselves with factories. Of course, in this simple example with just a single TU, the first function entered in that TU is `main` itself, and therefore _in this special case_ all globals must be initialized before `main`. – MSalters Aug 16 '16 at 09:34
  • @MSalters: You are certainly correct when it comes to C++ (and I rely on that), from memory you are correct in C too. – Bathsheba Aug 16 '16 at 09:54
2

Initialization of global variable happen before it entered into the main.

No you cannot declare global variable inside any function. If you declare any variable inside the function then scope of that variable will be limited to that function only. Instead of global variable you may try achieving your goal by using static variable.

Global variables are stored neither in stack nor in heap. Every program (executable code) is typically divided into four sections.

Code
Data
Stack
Heap

Global variables along with constants/literals are stored in the Data section. Source : Where does variables stored in memory?

P.S : static variable can be initialized only once.

Community
  • 1
  • 1
Shravan40
  • 8,922
  • 6
  • 28
  • 48