Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

LAB: 07 Topic: Malloc, Calloc, Realloc, Free, BRK and SBRK

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

19CS2106R​ Operating Systems Design​

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]]]*/

Operating Systems Design © 2020 KL University


Operating Systems Design © 2020 KL University
• Process Address Space
• When we compile and execute a
program, how different parts of the
program are mapped into memory?
• There are three areas where the program
gets mapped into the memory.
• The parts are as follows.
Stack:
• #include <stdio.h>
• int main(void)
• {
• int data; //local variable stored in stack
• return 0;
• }
• Code Segment (also known as Text segment)

– It has Executable statements of the program

• Data Segment

– Initialized Data: Statically allocated or global data that are initialized with non-
zero values

– Zero-Initialized or BSS data: Uninitialized data

– Heap: Source of Dynamic memory allocation

• Stack Segment

– Local variables of the scope.

– 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>

• int brk(void *end_data_segment);


• /* Returns 0 on success, or –1 on error */

• void *sbrk(intptr_t increment);


• /* Returns previous program break on
success, or (void *) –1 on error */
brk()
• It is a system call which sets the program break to the location specified
by end_data_segment.

• Since virtual memory is located in units of pages, end_data_segment is

effectively rounded up to the next page boundary.

Sbrk()

• A call to sbrk() adjusts the program break by adding an increment to it.

• sbrk() is a library function implemented on top of brk().

• On success, sbrk() returns the previous address of the program break.

• In other words, if we have increased the program break, then the return

value is a pointer to the start of the newly allocated block of memory.

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

You might also like