Global variables are stored on Heap and Heap is shared so both of them are shared.
Not able to find any "proper" references. Read this is in some book, forgot which one.
Here is a link for its reference:
http://cs.stackexchange.com/questions/48345/what-threads-share-in-general/48415
In practice global variables can be kept in the stack of Main function so that it can be accessed by all functions. Correction: Global variables cannot be inside a function, as that will limit its visibility.
The following argument is for when we need large memory, which is not asked in the question:
If its is kept in the main, the memory required for it will be taken from stack (whose size is fixed) and if the memory required is too much then this is will results into the famous error "Stack Overflow". However if it is outside any functions, then it is allocated from the heap or the data section of the code whose size can be configured as per the space required by the global variables.
Example:
This gives Stack overflow error:
int main(){
int arr[10000000];
cout<<"This is working"<<endl;
return 0;
}
This does not:
int arr[10000000];
int main(){
cout<<"This is working"<<endl;
return 0;
}
Thanks for the question. It taught me something which I took for granted. Hope this helps.