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

Intro To Programming Week 7

The document is a course outline for BCSC 1102: Intro to Programming, focusing on memory management, including sections of memory such as the stack and heap, and dynamic memory allocation techniques like malloc and calloc. It emphasizes the importance of understanding memory management for writing efficient and error-free code. The content is structured into sections detailing memory layout, operations, and examples of dynamic memory usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Intro To Programming Week 7

The document is a course outline for BCSC 1102: Intro to Programming, focusing on memory management, including sections of memory such as the stack and heap, and dynamic memory allocation techniques like malloc and calloc. It emphasizes the importance of understanding memory management for writing efficient and error-free code. The content is structured into sections detailing memory layout, operations, and examples of dynamic memory usage.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

BCSC 1102 : Intro To Programming Week 7

Dr. Shem Mbandu Angolo, PhD

The Co-operatetive University of Kenya


September - December 2024

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

3 Dynamic Memory Allocation 4


3.1 malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 calloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.3 free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.4 Example: Dynamic Array . . . . . . . . . . . . . . . . . . . . . . 5

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.

– Initialized Data Segment: Stores global and static variables that


are initialized with a value.
– Uninitialized Data Segment (BSS): Stores global and static vari-
ables that are not initialized.

• 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

Figure 1: Memory Layout: Text, Data, BSS, Heap, and Stack

2.2 The Stack


The stack is a region of memory used for static memory allocation. It stores:
• Local variables

• 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 }

2.3 The Heap


The heap is a region of memory used for dynamic memory allocation. Unlike
the stack, the heap allows for memory to be allocated and deallocated in an
arbitrary order.

2.3.1 Heap Operations


Memory on the heap must be managed manually using functions such as malloc,
calloc, and free.
1 // Example of heap usage
2 # include < stdlib .h >
3

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

11 // Use the allocated memory


12 for ( int i = 0; i < 5; i ++) {
13 p [ i ] = i * 10;

3
14 }
15

16 free ( p ) ; // Free the allocated memory


17 return 0;
18 }

2.4 Other Memory Sections


2.4.1 Text (Code) Segment
The text segment contains the compiled program code. It is typically read-only
to prevent accidental modification of instructions.

2.4.2 Data Segment


The data segment is divided into two parts:
• Initialized Data Segment: Stores global and static variables that are
initialized with a value.
• Uninitialized Data Segment (BSS): Stores global and static variables
that are not initialized.

3 Dynamic Memory Allocation


Dynamic memory allocation is essential for handling memory at runtime. It
allows programs to allocate memory as needed, rather than relying solely on
static memory allocation.

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 int * p = ( int *) malloc ( sizeof ( int ) * 5) ;


4 if ( p == NULL ) {
5 // Handle memory allocation failure
6 }

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 int * p = ( int *) calloc (5 , sizeof ( int ) ) ;


4 if ( p == NULL ) {
5 // Handle memory allocation failure
6 }

3.3 free
The free function deallocates the memory previously allocated by malloc,
calloc, or realloc.
1 # include < stdlib .h >
2

3 free ( p ) ; // Deallocate the memory

3.4 Example: Dynamic Array

1 # include < stdio .h >


2 # include < stdlib .h >
3

4 int main () {
5 int n ;
6 printf (" Enter the number of elements : ") ;
7 scanf ("% d " , & n ) ;
8

9 int * arr = ( int *) malloc ( n * sizeof ( int ) ) ;


10 if ( arr == NULL ) {
11 printf (" Memory allocation failed \ n ") ;
12 return 1;
13 }
14

15 for ( int i = 0; i < n ; i ++) {


16 arr [ i ] = i + 1;
17 }
18

19 printf (" Array elements : ") ;


20 for ( int i = 0; i < n ; i ++) {
21 printf ("% d " , arr [ i ]) ;
22 }
23 printf ("\ n ") ;
24

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.

You might also like