Dynamic Memory Allocation
Dynamic Memory Allocation
Allocation
1
Memory Layout of C Program
In practical words, when we run any C-program, its
executable image is loaded into RAM of computer in an
organized manner.
2
Memory Layout of C Program
1>Text or Code Segment :-Text segment
contains machine code of the compiled
program. The text segment of an executable
object file is often read-only segment that
prevents a program from being accidentally
modified.
3
Memory Layout of C Program
2>Initialized Data Segment :-Initialized data
stores all global, static, constant, and
external variables (declared with extern
keyword ) that are initialized beforehand.
Data segment is not read-only, since the
values of the variables can be altered at run
time.
4
Memory Layout of C Program
3>Uninitialized Data Segment (bss):-Data in
this segment is initialized to arithmetic 0
before the program starts executing.
Uninitialized data starts at the end of the data
segment and contains all global variables and
static variables that are initialized to 0 or do
not have explicit initialization in source code.
5
Memory Layout of C Program
4>Heap:-Heap is the segment where
dynamic memory allocation usually takes
place. When some more memory need to be
allocated using malloc and calloc function,
heap grows upward. The Heap area is
shared by all shared libraries and
dynamically loaded modules in a process.
6
Memory Layout of C Program
5>Stack:-Stack segment is used to store all local variables
and is used for passing arguments to the functions along
with the return address of the instruction which is to be
executed after the function call is over. Local variables have
a scope to the block which they are defined in, they are
created when control enters into the block. All recursive
function calls are added to stack.
7
Problem with Arrays
Sometimes
Amount of data cannot be predicted beforehand
Number of data items keeps changing during program execution
Example: Search for an element in an array of N elements
One solution: find the maximum possible value of N and allocate
an array of N elements
Wasteful of memory space, as N may be much smaller in some
executions
Example: maximum value of N may be 10,000, but a particular
run may need to search only among 100 elements
Using array of size 10,000 always wastes memory in most
cases
8
Better Solution
Dynamic memory allocation
Know how much memory is needed after the program
is run
Example: ask the user to enter from keyboard
Dynamically allocate only the amount of memory
needed
C provides functions to dynamically allocate
memory
malloc, calloc, realloc
9
Memory Allocation Functions
malloc
Allocates requested number of bytes and returns a
pointer to the first byte of the allocated space
calloc
Allocates space for an array of elements, initializes
them to zero and then returns a pointer to the
memory.
free
Frees previously allocated space.
realloc
Modifies the size of previously allocated space.
10
Allocating a Block of Memory
A block of memory can be allocated using the
function malloc
Reserves a block of memory of specified size
and returns a pointer of type void
The return pointer can be type-casted to any
pointer type
General format:
type *p;
p = (type *) malloc (byte_size);
11
Example
p = (int *) malloc(100 * sizeof(int));
13
Points to Note
malloc always allocates a block of contiguous bytes
The allocation can fail if sufficient contiguous
memory space is not available
If it fails, malloc returns NULL
14
Using the malloc’d Array
Once the memory is allocated, it can be used with
pointers, or with array notation
Example:
int *p, n, i;
scanf(“%d”, &n);
p = (int *) malloc (n * sizeof(int));
for (i=0; i<n; ++i)
scanf(“%d”, &p[i]);
16
Allocation Examples
p1 = malloc(4*sizeof(int))
p2 = malloc(5*sizeof(int))
p3 = malloc(6*sizeof(int))
free(p2)
p4 = malloc(2*sizeof(int))