Operating Systems
Youjip Won
14. Memory API
Youjip Won 2
Overview
� malloc/free
� calloc/realloc
� brk/sbrk
� mmap/mumap
Youjip Won 3
Virtual Address Space
libc
malloc/free brk/sbrk/mmap
Application Operating
System
Code
(Text)
mmap
Data
malloc() Heap 4KB
free()
calloc() brk()
realloc() sbrk()
Heap
in libc
(free) To change the heap size
To manage the
memory in a
heap 4KB
Stack
Youjip Won 4
malloc()
#include <stdlib.h>
void* malloc(size_t size)
� Allocate a memory region on the heap.
⬥ Argument
� size_t size : size of the memory block(in bytes)
� size_t is an unsigned integer type.
⬥ Return
� Success : a void type pointer to the memory block allocated by malloc
� Fail : a null pointer
Youjip Won 5
sizeof()
� Routines and macros are utilized for size in malloc instead typing
in a number directly.
� Two types of results of sizeof with variables
⬥ The actual size of ‘x’ is known at run-time.
int *x = malloc(10 * sizeof(int));
printf(“%d\n”, sizeof(x));
⬥ The actual size of ‘x’ is known at compile-time.
int x[10];
printf(“%d\n”, sizeof(x));
40
Youjip Won 6
Memory API: free()
#include <stdlib.h>
void free(void* ptr)
� Free a memory region allocated by a call to malloc.
⬥ Argument
� void *ptr : a pointer to a memory block allocated with malloc
⬥ Return
� none
Youjip Won 7
Memory Allocating
2KB
pointer
heap
(free)
stack int *pi; // local variable
*pi
16KB
Address Space
2KB
allocated
2KB + 4
allocated
2KB + 8
allocated
2KB + 12 pi = (int *)malloc(sizeof(int)*
allocated 4);
(free)
*pi
16KB
Address Space
Youjip Won 8
Memory Freeing
2KB
freed
2KB + 4
freed
2KB + 8
freed
2KB + 12 free(pi);
freed
(free)
2KB(invalid) *pi
16KB
Address Space
2KB
heap
(free)
stack
2KB(invalid) *pi
16KB
Address Space
Youjip Won 9
Forgetting To Allocate Memory
� Incorrect code
char *src = “hello”; //character string constant
char *dst; //unallocated
strcpy(dst, src); //segfault and die
hello\0
heap
strcpy(dst, src); (free) unallocated
stack
*dst
*src
Address Space
Youjip Won 10
Forgetting To Allocate Memory(Cont.)
� Correct code
char *src = “hello”; //character string constant
char *dst (char *)malloc(strlen(src) + 1 ); // allocated
strcpy(dst, src); //work properly
hello\0 hello\0
allocated hello\0
strcpy(dst, src); heap heap
(free) (free)
stack stack
*dst *dst
*src *src
Address Space Address Space
Youjip Won 11
Not Allocating Enough Memory
� Incorrect code, but work properly
char *src = “hello”; //character string constant
char *dst (char *)malloc(strlen(src)); // too small
strcpy(dst, src); //work properly
h
e
strlen l
6 bytes
l
o
\0
‘\0’ is omitted 5 bytes hello\0
strcpy(dst, src); heap
(free)
stack
*dst
*src
Address Space
Youjip Won
Forgetting to Initialize
� Encounter an uninitialized read
int *x = (int *)malloc(sizeof(int)); // allocated
printf(“*x = %d\n”, *x); // uninitialized memory access
value used allocated
before with value used
(free) before
heap heap
(free) (free)
stack stack
*x *x
Address Space Address Space
Youjip Won 13
Memory Leak
� A program keeps allocating memory without freeing it.
� A program runs out of memory and eventually is killed by OS.
while(1)
malloc(4) ;
unused : allocated, but not freed
allocated unused unused
allocated unused
heap heap
unused
heap
(free) allocated
(free)
stack (free)
stack *d
*c
*b *b
*a *a *a
run out of memory
Youjip Won 14
Dangling Pointer
� Freeing memory while it is being used.
⬥ A program accesses to memory with an invalid pointer
*b free()
*b unreachable
*a *a
dangling pointer
2KB 2KB
3KB 3KB
3KB 4KB 3KB
freed
free(b
4KB ) 4KB
NULL NULL
Heap Heap
(free) (free)
Stack Stack
*b 3KB *b 3KB
*a 2KB *a 2KB
Address Space Address Space
Youjip Won 15
Incorrect free()
� Free the memory that was freed already.
int *x = (int *)malloc(sizeof(int)); // allocated
free(x); // free memory
free(x); // free repeatedly
2KB 2KB
allocated freed
Heap Heap
free(x free(x)
) We don’t know
(free) (free)
what will happen.
Stack Stack
2KB *x 2KB(invalid)
16KB 16KB *x
Address Space Address Space
� Free the memory that was not allocated via malloc().
int *x = (int *)malloc(sizeof(int)); // allocated
free(x+12); // free memory
Youjip Won 16
Other Memory APIs: calloc() and realloc()
#include <stdlib.h>
void *calloc(size_t num, size_t size)
� Allocate memory and zeroes it before returning.
⬥ size_t num : the number of objects to allocate
⬥ size_t size : size of an ojbect (in bytes)
#include <stdlib.h>
void *realloc(void *ptr, size_t size)
� Change the size of memory block.
⬥ void *ptr: Pointer to memory block allocated with malloc, calloc or
realloc
⬥ size_t size: New size for the memory block(in bytes)
Youjip Won 17
System Calls
#include <unistd.h>
Address Space
int brk(void *addr) Code
void *sbrk(intptr_t increment); (Text)
� There lacks of heap space. 🡪 Ask Data
OS to expand heap. Heap 4KB
� break: The location of the end of
brk() Operating
the heap in address space sbrk()
System
(free)
� malloc uses brk system call. To change
the heap
⬥ brk is called to expand the size
program’s break.
⬥ sbrk is similar to brk.
Stack 4KB
⬥ Programmers should never
directly call either brk or sbrk.
18
Youjip Won
System Calls: mmap
#include <sys/mman.h>
void *mmap(void *ptr, size_t length, int prot, int flags,
int fd, off_t offset)
� Allocate a memory region of length at ptr.
� If fd is not negative, associate the region to fd starting at offset.
Youjip Won 19
mmap: creating file-backed region
Address Space
Code ptr = mmap(0, 40, flag, MAP_SHARED, fd, 0)) ;
(Text) if (ptr == MAP_FAILED)
exit(EXIT_FAILURE);
Data
Heap
ptr
File-backed
40 B
region fd
Stack
Youjip Won 20
mmap: creating anonymous region
#include <sys/mman.h>
void *mmap(void *ptr, size_t length, int
prot, int flags, int fd, off_t offset)
Address Space
Code
(Text) ptr= mmap(NULL, 40, PROT_READ |
PROT_WRITE, MAP_SHARED |
Data MAP_ANONYMOUS, -1, 0);
Heap if (ptr == MAP_FAILED)
exit(EXIT_FAILURE);
ptr
anonymous
40 B
region
Stack
Youjip Won 21
Summary
� malloc/free
� calloc/realloc
� mmap/munmap
Youjip Won 22