Intro To Programming Week 7
Intro To Programming Week 7
Contents
1 Introduction to Memory Management 2
2 Sections of Memory 2
2.1 Memory Layout Diagram . . . . . . . . . . . . . . . . . . . . . . 2
2.2 The Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2.1 Stack Operations . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 The Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3.1 Heap Operations . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 Other Memory Sections . . . . . . . . . . . . . . . . . . . . . . . 4
2.4.1 Text (Code) Segment . . . . . . . . . . . . . . . . . . . . 4
2.4.2 Data Segment . . . . . . . . . . . . . . . . . . . . . . . . . 4
4 Conclusion 6
1
1 Introduction to Memory Management
In programming, understanding how memory is managed is crucial for writ-
ing efficient and error-free code. Memory is divided into several sections, each
serving a specific purpose.
2 Sections of Memory
2.1 Memory Layout Diagram
Below is a description of the different sections of memory used during code
execution. The image shows a typical memory layout in a program:
• Text (Code) Segment: Contains the compiled program code. This sec-
tion is typically read-only to prevent accidental modification of instruc-
tions.
• Data Segment: Divided into initialized and uninitialized (BSS) seg-
ments.
• Heap: Used for dynamic memory allocation. Memory in the heap must
be managed manually using functions such as malloc, calloc, and free.
• Stack: Used for static memory allocation. It stores local variables, func-
tion parameters, and return addresses. Operates in a Last In, First Out
(LIFO) manner.
[width=0.8]memoryl ayout.png
• Function parameters
• Return addresses
2
2.2.1 Stack Operations
The stack operates in a Last In, First Out (LIFO) manner. Functions use the
stack to manage local variables and return addresses.
1 // Example of stack usage in functions
2 void functionA () {
3 int a = 5; // Local variable ’a ’ stored on the
stack
4 functionB () ;
5 }
6
7 void functionB () {
8 int b = 10; // Local variable ’b ’ stored on the
stack
9 }
10
11 int main () {
12 functionA () ;
13 return 0;
14 }
4 int main () {
5 int * p = ( int *) malloc ( sizeof ( int ) * 5) ; //
Allocate memory on the heap
6 if ( p == NULL ) {
7 // Handle memory allocation failure
8 return 1;
9 }
10
3
14 }
15
3.1 malloc
The malloc function allocates a specified number of bytes and returns a pointer
to the allocated memory. The memory is uninitialized.
1 # include < stdlib .h >
2
3.2 calloc
The calloc function allocates memory for an array of elements, initializes the
memory to zero, and returns a pointer to the allocated memory.
4
1 # include < stdlib .h >
2
3.3 free
The free function deallocates the memory previously allocated by malloc,
calloc, or realloc.
1 # include < stdlib .h >
2
4 int main () {
5 int n ;
6 printf (" Enter the number of elements : ") ;
7 scanf ("% d " , & n ) ;
8
25 free ( arr ) ;
26 return 0;
5
27 }
4 Conclusion
Understanding memory management and dynamic memory allocation is cru-
cial for efficient programming. Properly using the stack and heap, along with
dynamic memory functions like malloc, calloc, and free, ensures optimal
memory utilization and program stability.