LAB: 07 Topic: Malloc, Calloc, Realloc, Free, BRK and SBRK
LAB: 07 Topic: Malloc, Calloc, Realloc, Free, BRK and SBRK
LAB: 07 Topic: Malloc, Calloc, Realloc, Free, BRK and SBRK
LAB : 07
Topic : Malloc(), Calloc, realloc(),free(), brk() and sbrk()
© 2020 KL University
• 7. 1
• /* It prints the addresses of all the segments and the
address of variables residing in their respective
segments. segments-during-runtime.c */
• 7(2)
• * free_and_sbrk.c
• Test if free(3) actually lowers the program break.
• Usage: free_and_sbrk num-allocs block-size [step [min
[max]]]*/
• Data Segment
– Initialized Data: Statically allocated or global data that are initialized with non-
zero values
• Stack Segment
– Function parameters
– Variables stores on the stack "disappear" when the function containing them
returns, the space on the stack is reused for subsequent function calls.
Memory Layout
Top of stack
Program break
Heap:
• #include <stdio.h>
• int main(void)
• {
• char *pStr = malloc(sizeof(char)*4);
Click to add text
//stored in heap
• return 0;
• }
BSS - Block started by Symbol
(Uninitialized data segment):
• #include <stdio.h>
• int data1; // Uninitialized global variable stored in BSS
• int main(void)
• {
• static int data2; // Uninitialized static variable stored in BSS
• return 0;
• }
DS(Initialized data segment):
• #include <stdio.h>
• int data1 = 10 ; //Initialized global variable stored in DS
• int main(void)
• {
• static int data2 = 3; //Initialized static variable stored in DS
• return 0;
• }
Text:
• #include <stdio.h>
• int main(void)
• {
• return 0;
• }
• C provides some functions to achieve these tasks.
• There are 4 library functions provided by C defined
under <stdlib.h> header file to facilitate dynamic memory
allocation in C programming. They are:
• malloc()
• calloc()
• free()
• realloc()
malloc() method
• “malloc” or “memory allocation” method is used to dynamically allocate a
single large block of memory with the specified size.
• It returns a pointer of type void which can be cast into a pointer of any form.
• It initializes each block with default garbage value.
Syntax:
• ptr = (cast-type*) malloc(byte-size)For Example:
• ptr = (int*) malloc(100 * sizeof(int));
• Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory.
• And, the pointer ptr holds the address of the first byte in the allocated memory.
free() method
• “free” method is used to dynamically de-allocate the
memory.
• The memory allocated using functions malloc() and
calloc() is not de-allocated on their own.
• Hence the free() method is used.
• It helps to reduce wastage of memory by freeing it.
Syntax:
• free(ptr);
If space is insufficient, allocation fails and returns a
NULL pointer.
calloc() method
• “calloc” or “contiguous allocation” method is used to
dynamically allocate the specified number of blocks of
memory of the specified type.
• It initializes each block with a default value ‘0’.
Syntax:
• ptr = (cast-type*)calloc(n, element-size);For Example:
• ptr = (float*) calloc(25, sizeof(float));
• This statement allocates contiguous space in memory for
25 elements each with the size of the float.
If space is insufficient, allocation fails and returns a
NULL pointer.
realloc() method
• “realloc” or “re-allocation” method is used to dynamically change
the memory allocation of a previously allocated memory.
• In other words, if the memory previously allocated with the help of
malloc or calloc is insufficient, realloc can be used to dynamically
re-allocate memory.
• re-allocation of memory maintains the already present value and
new blocks will be initialized with default garbage value.
Syntax:
• ptr = realloc(ptr, newSize);
• where ptr is reallocated with new size 'newSize'.
If space is insufficient, allocation fails and returns a
NULL pointer.
• A process can allocate memory by increasing the size of
the heap.
• Heap is a variable-size segment of contiguous virtual
memory that begins just after the uninitialized data
segment of a process and grows & shrinks as memory
allocated and freed.
Click to add text
• The current limit of the heap referred to as the program
break which is just at the end of the uninitialized data
segment in process address space.
• Resizing the heap (i.e., allocating or de-allocating memory)
is as simple as telling the kernel to adjust its idea of where
the process’s program break is.
brk() & sbrk()
• #include <unistd.h>
Sbrk()
• In other words, if we have increased the program break, then the return
• The call sbrk(0) returns the current setting of the program break without
changing it.
• This can be useful if we want to track the size of the heap.