You are on page 1of 24

BITS Pilani

K K Birla Goa Campus

CSF111
Computer Programming

Dynamic Memory Allocation


(Supplementary)
Dynamic Memory Allocation Examples
✤ malloc
Dynamic Memory Allocation Examples
✤ malloc
Dynamic Memory Allocation Examples
✤ calloc
Dynamic Memory Allocation Examples
✤ calloc
Dynamic Memory Allocation Examples
✤ free

An allocated block can be returned to the system for future use, by the free function.

General syntax:
free (ptr);

where ptr is a pointer to a memory block which has been previously created using
malloc (or calloc or realloc).

No size is to be mentioned for the allocated block. The system remembers it. The function frees
the entire block allocated by an earlier malloc() type of call.

ptr must be the starting address of an allocated block. A pointer to the interior of a block cannot
be passed to free().
Dynamic Memory Allocation Examples
✤ free
Dynamic Memory Allocation Examples
✤ realloc

Sometimes we need to alter the size of some previously allocated memory block.

• More memory needed.


• Memory allocated is larger than necessary.

How?
• By using the realloc function.

If the original allocation is done as:


ptr = malloc (size);

then reallocation of space may be done as:


ptr = realloc (ptr, newsize);
Dynamic Memory Allocation Examples
✤ realloc

If the original allocation is done as:


ptr = malloc (size);

then reallocation of space may be done as:


ptr = realloc (ptr, newsize);

• The new memory block may or may not begin at the same place as the old one.

• If it does not find space, it will create it in an entirely different region and move the
contents of the old block into the new block.

• The function guarantees that the old data remains intact.

• If it is unable to allocate, it returns NULL and frees the original block.


Dynamic Memory Allocation Examples
✤ realloc
Dynamic Memory Allocation Examples
What will be the output of the code segment?
Dynamic Memory Allocation Examples
What will be the output of the code segment?

Type casted to Int* for char


Dynamic Memory Allocation Examples
What will be the output of the code segment?
Dynamic Memory Allocation Examples
What will be the output of the code segment?

assignment makes pointer from integer


without a cast
Dynamic Memory Allocation Examples
Sum of n elements in an array (created using dynamic memory allocation)
Dynamic Memory Allocation Examples
Create a set of student records and display those
Dynamic Memory Allocation Examples
Create a set of student records and display those
Dynamic Memory Allocation Examples
Create a set of student records and display those
Dynamic Memory Allocation Examples
Create a set of student records and display those

How to optimize?
Dynamic Memory Allocation Examples
Create a set of student records and display those

Use a pointer to a
pointer

*Footnote (aid for understanding)

p_ptr = &temp
*p_ptr = temp
*(p_ptr+0) = temp
p_ptr[0] = temp
generic version →p_ptr[i] = temp;
Dynamic Memory Allocation Examples
Create a table to store the marks of m subjects of n students. (assume marks are integer number)

Student M1 M2 M3 … Mm
S1
S2
S3
..
..
..
Sn
Dynamic Memory Allocation Examples
Create a table to store the marks of m subjects of n students. (assume marks are integer number)
Dynamic Memory Allocation Examples
Create a table to store the marks of m subjects of n students. (assume marks are integer number)
Dynamic Memory Allocation Examples
Create a table to store the marks of m subjects of n students. (assume marks are integer number)

You might also like