0% found this document useful (0 votes)
2 views21 pages

14 MemoryAPI

Uploaded by

donggyuhun
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

14 MemoryAPI

Uploaded by

donggyuhun
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

14.

Memory API

1
Overview

 malloc/free

 calloc/realloc

 brk/sbrk

 mmap/mumap

Soon Hwang 2
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
※ malloc/free는 libc 라이브러리 함수. brk/sbrk/mmap은 OS 시스템 콜. 프로그래머는 보통 malloc/free만 사용

Soon Hwang 3
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

Soon Hwang 4
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

Soon Hwang 5
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

Soon Hwang 6
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)

*pi
16KB
Address Space

Soon Hwang 7
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

Soon Hwang 8
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

Soon Hwang 9
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

Soon Hwang 10
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

Soon Hwang
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

Soon Hwang 12
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
※ Memory Leak =*a *a
할당 후 free를 안 하는 것. 장시간 실행되는 서버 프로그램에서 특히 위험 *a

run out of memory


Soon Hwang 13
Dangling Pointer

 Freeing memory while it is being used.


 A program accesses to memory with an invalid pointer

*b *b free()
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

Soon Hwang 14
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

Soon Hwang 15
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 object (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)

Soon Hwang 16
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.

17
Soon Hwang
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.

Soon Hwang 18
mmap: creating file-backed region

Address Space
Code ptr = mmap(0, 40, flag, MAP_SHARED, fd, 0)) ;
(Text) if (ptr == MAP_FAILED)
Data exit(EXIT_FAILURE);

Heap

ptr
File-backed
40 B region fd

Stack

Soon Hwang 19
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

Soon Hwang 20
Summary

 malloc/free

 calloc/realloc

 mmap/munmap

Soon Hwang 21

You might also like