14.
Memory API
Operating System: Three Easy Pieces
1
Memory API: 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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
2
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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
3
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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
4
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
allocated pi = (int *)malloc(sizeof(int)* 4);
(free)
2KB *pi
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
5
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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
6
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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
7
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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
8
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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
10
Memory Leak
A program runs out of memory and eventually dies.
unused : unused, 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
Address Space Address Space Address Space
run out of memory
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
11
Dangling Pointer
Freeing memory before it is finished using
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
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
12
Other Memory APIs: calloc()
#include <stdlib.h>
void *calloc(size_t num, size_t size)
Allocate memory on the heap and zeroes it before returning.
Argument
size_t num : number of blocks to allocate
size_t size : size of each block(in bytes)
Return
Success : a void type pointer to the memory block allocated by calloc
Fail : a null pointer
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
13
Double Free
Free 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)
Undefined
(free) (free)
Error
Stack Stack
2KB *x 2KB(invalid)
16KB 16KB *x
Address Space Address Space
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
14
Other Memory APIs: realloc()
#include <stdlib.h>
void *realloc(void *ptr, size_t size)
Change the size of memory block.
A pointer returned by realloc may be either the same as ptr or a new.
Argument
void *ptr: Pointer to memory block allocated with malloc, calloc or
realloc
size_t size: New size for the memory block(in bytes)
Return
Success: Void type pointer to the memory block
Fail : Null pointer
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
15
System Calls
#include <unistd.h>
int brk(void *addr)
void *sbrk(intptr_t increment);
malloc library call use brk system call.
brk is called to expand the program’s break.
break: The location of the end of the heap in address space
sbrk is an additional call similar with brk.
Programmers should never directly call either brk or sbrk.
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
16
System Calls(Cont.)
#include <sys/mman.h>
void *mmap(void *ptr, size_t length, int port, int flags,
int fd, off_t offset)
mmap system call can create an anonymous memory region.
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur
17
Disclaimer: This slide set has been adapted from the initial lecture slides for Operating
System course in Computer Science Dept. at Hanyang University. This lecture slide set is
for OSTEP book written by Remzi and Andrea at University of Wisconsin.
Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 18