zaro

Where are global variables stored?

Published in Computer Memory 2 mins read

Global variables are stored in the data segment of memory, which is part of the address space allocated to a process. Specifically, they reside within the data segment of a shared-object file and are associated with the virtual processor (VP) and not a specific thread.

Here's a breakdown for better understanding:

  • Address Space: Each running program (process) gets its own dedicated address space, a range of virtual memory addresses it can use. This prevents programs from interfering with each other's data.

  • Data Segment: Within this address space, the data segment is where initialized global and static variables are stored. This segment is typically read-write, allowing the values of these variables to be modified during program execution.

  • Global Variables: These variables are declared outside of any function and are accessible from anywhere in the program.

  • Static Variables: These variables have a scope limited to the file in which they are defined. Static global variables can only be accessed by functions within the same source file, but maintain their value across multiple function calls. Static local variables are only visible within the function in which they are declared but retain their value between calls to that function.

  • Virtual Processor (VP): In a multi-processing or multi-threading environment, global and static variables are tied to a virtual processor's address space rather than an individual thread. This has implications for cross-VP migration safety.

Implications for Multi-threading and Multi-processing:

It's crucial to understand the context of global variables in concurrent programming:

  • Data Races: Since global variables are accessible from multiple threads or processes, modifying them simultaneously can lead to data races. Proper synchronization mechanisms (like mutexes, semaphores, or atomic operations) are required to protect global variables and ensure data integrity.

  • VP Migration: Because global variables are linked to the address space of a virtual processor, modifications to them may not be safe across VP migration boundaries, so proper locking mechanisms should be in place.

Example (C/C++):

#include <iostream>

int global_variable = 10; // Declared outside any function

void myFunction() {
  static int static_variable = 0; // Retains value between calls
  static_variable++;
  std::cout << "Global Variable: " << global_variable << std::endl;
  std::cout << "Static Variable: " << static_variable << std::endl;
}

int main() {
  myFunction(); // global_variable = 10, static_variable = 1
  myFunction(); // global_variable = 10, static_variable = 2
  global_variable = 20;
  myFunction(); // global_variable = 20, static_variable = 3
  return 0;
}

In this example, global_variable is stored in the data segment and its value can be changed from anywhere. static_variable is also stored in the data segment (specifically the uninitialized data segment, or BSS, if not explicitly initialized) but its scope is limited to myFunction.