2. MEMORY MODEL
• Defines the semantics of computer memory storage for the purpose
of the C abstract machine.
• The data storage (memory) available to a C program is one or
more contiguous sequences of bytes.
• Each byte in memory has a unique address.
• Pointers present us with a certain abstraction of the environment
and state in which our program is executed, the C memory model.
3.
4. SECTIONS OF MEMORY
• A typical memory representation of a C program consists of
the following sections.
1. Text segment (i.e. instructions)
2. Initialized data segment
3. Uninitialized data segment (bss)
4. Heap
5. Stack
5.
6. TEXT SEGMENT
• A text segment, also known as a code segment or simply as text, is one
of the sections of a program in an object file or in memory, which
contains executable instructions.
• As a memory region, a text segment may be placed below the heap or
stack in order to prevent heaps and stack overflows from overwriting
it.
• Usually, the text segment is sharable so that only a single copy needs
to be in memory for frequently executed programs, such as text
editors, the C compiler, the shells, and so on.
7. DATA SEGMENT
• The data which we use in our program will be stored in the data
section.
• Since the variables declared inside the main() function are stored in
the stack, but the variables declared outside the main() method will be
stored in the data section.
• The variables declared in the data section could be stored in the form
of initialized, uninitialized, and it could be local or global.
• Therefore, the data section is divided into four categories, i.e.,
initialized, uninitialized, global, or local.
8. EXAMPLE
int var1;
int var2 = 10;
void function1()
{
printf("I am function1");
}
int main()
{
function1();
return 0;
}
• Here, var1 and var2 variables are declared
outside the main() function where var1 is
the uninitialized variable, whereas the
var2 is an initialized variable.
• These variables can be accessed anywhere
in the program because these variables are
not a part of the main() in the stack.
9. DATA SEGMENT
• The data segment consists of two segments:
1. Uninitialized data segment: The uninitialized data segment is also
known as a .bss segment that stores all the uninitialized global, local
and external variables. The .bss segment stands for Block Started by
symbol.
char a; // uninitialized global variable..
int main()
{
static int a; // uninitialized static variable..
return 0;
}
10. DATA SEGMENT
• Initialized data segment: An initialized data segment is also known as
the data segment. A data segment is a virtual address space of a program
that contains all the global and static variables which are explicitly
initialized by the programmer.
char string[] = "javatpoint"; // global variable stored in initiali
zed data segment in read-write area..
int main()
{
static int i = 90; // static variable stored in initialized data seg
ment..
return 0;
}
11. STACK
• The stack area traditionally adjoined the heap area and grew in the
opposite direction; when the stack pointer met the heap pointer, free
memory was exhausted.
• When we define a function and call that function then we use the stack
frame.
• The stack section plays a very important role in the memory because
whenever the function is called, a new stack frame is created.
• Stack is also used for recursive functions.
12. HEAP
• Heap memory is used for the dynamic memory allocation.
Heap memory begins from the end of the uninitialized data
segment and grows upwards to the higher addresses.
• The malloc() and calloc() functions are used to allocate the
memory in the heap.
• The heap memory can be used by all the shared libraries and
dynamically loaded modules.
• The free() function is used to deallocate the memory from the
heap.