0% found this document useful (0 votes)
14 views

PC1 ProgramMemory

Program memory is typically organized into segments like code, data, BSS, and stack. The code segment stores executable program statements. The data segment stores initialized global variables and constants. The BSS segment stores uninitialized global variables. The stack stores local variables, function parameters, and return addresses for function calls. Memory on the heap is dynamically allocated and remains allocated until explicitly freed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

PC1 ProgramMemory

Program memory is typically organized into segments like code, data, BSS, and stack. The code segment stores executable program statements. The data segment stores initialized global variables and constants. The BSS segment stores uninitialized global variables. The stack stores local variables, function parameters, and return addresses for function calls. Memory on the heap is dynamically allocated and remains allocated until explicitly freed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Program Memory

Structured Programming

Dr. M. Talla
Program memory
Memory is typically organized in segments - regions with a starting and an
ending address. OS allocates memory segments to a program, makes them
read/write protected, and assigns to variables while running the program.
A program memory is usually organized as follows:

CODE DATA BSS STACK HEAP

Block Started by Symbol


(for un-initialized global
data, static variables)

Executable programs global


code All the local
variables are. variables The rest
(statements)
are stored
Memory Allocation
BSS – Block Started by Symbol (for un-initialized global data)
Data: Initialized global data, Un-initialized global data,
Initialized local data, Un-initialized local data
Example:
int abc = 1; // Initialized global data - Read-Write Data
char *str; // BSS for un-initialized global data
const int i = 10; // Initialized Read-Only Data
main() {
int ii, a=1,b=2,c; // Local Variables on Stack
char *ptr; // ptr itself is on stack
ptr = malloc(4); // Allocated Memory in Heap, its address stored in ptr
c= a + b;
}
The Stack
So much memory is assigned to a program and used it
during execution. SP “Stack Pointer” register points
current stack address. Storage is “on Top”. Retrieval is
“First-in, Last-out” (or Last-in First-out).
Upon function call instruction, return address is stored
on stack memory and all it’s local variables are also
allocated on the stack.
Further more, the program can use “push” instruction to
store data on stack, and “pop” instruction to retrieve it.
Stack memory allocated for a function call is
automatically released upon “return” instruction or upon
end-of-block curly brace.
The Stack (cont)
The number of push and pop instructions in your
program should match, or you should know what
you are doing in your program.
If you try to store too much data on stack (too
many function calls or too much local variables),
you can create “Stack-Over-Flow”.
If you use more “pop” instructions than your
“push” instructions, you can go below the stack
and create “stack underflow” error. Both of these
errors are fatal and OS is kind to detect these
errors and crash your program.
The Heap

Heap is basically the rest of memory.


❖Memory allocated on heap, is held till a
specific instruction to release that memory.
❖This is the source of memory leaks in C++ if
not released or if the pointer to memory is
lost (or corrupted). Java has a garbage
collector, that automatically cleans up any
unreferenced memory.

You might also like