Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global memory management in C++ in stack or heap?

If I declare a data structure globally in a C++ application , does it consume stack memory or heap memory ?

For eg

struct AAA {  .../.../. ../../.. }arr[59652323]; 
like image 570
sameer karjatkar Avatar asked Jul 23 '09 05:07

sameer karjatkar


People also ask

Is global memory on the heap?

Global data structures or global variables are not consumed by stack or heap. They basically allocated in a fixed memory block, which remains unchanged.

Where are global variables stored in C stack or heap?

Global variables are stored in the data section. Unlike the stack, the data region does not grow or shrink — storage space for globals persists for the entire run of the program. Finally, the heap portion of memory is the part of a program's address space associated with dynamic memory allocation.

Does C use heap memory?

in C, variables are allocated and freed using functions like malloc() and free() the heap is large, and is usually limited by the physical memory available. the heap requires pointers to access it.

What is stack and heap memory in C?

The major difference between Stack memory and heap memory is that the stack is used to store the order of method execution and local variables while the heap memory stores the objects and it uses dynamic memory allocation and deallocation.


1 Answers

Since I wasn't satisfied with the answers, and hope that the sameer karjatkar wants to learn more than just a simple yes/no answer, here you go.

Typically a process has 5 different areas of memory allocated

  1. Code - text segment
  2. Initialized data – data segment
  3. Uninitialized data – bss segment
  4. Heap
  5. Stack

If you really want to learn what is saved where then read and bookmark these:

COMPILER, ASSEMBLER, LINKER AND LOADER: A BRIEF STORY (look at Table w.5)

Anatomy of a Program in Memory

alt text

like image 53
Milan Avatar answered Sep 24 '22 10:09

Milan