3 1 Memory
3 1 Memory
Topic: Memory
Endadul Hoque
Acknowledgement
Current
Program
(code, data, etc.)
max
Physical Memory
Multiprogramming and Time Sharing
0KB
• Load multiple processes in memory. Operating System
(code, data, etc.)
64KB
– Execute one for a short while.
Free
– Switch between processes in memory. 128KB
Process C
– Increase utilization and efficiency. (code, data, etc.)
192KB
Process B
(code, data, etc.)
• Cause an important protection issue. 256KB
Free
– Errant memory accesses 320KB
Process A
from other processes (code, data, etc.)
384KB
Free
448KB
Free
512KB
Physical Memory
Address Space
• OS creates an abstraction of physical memory.
– The address space contains the content of 0KB
Program Code
an active (precisely, alive) process.
1KB
– That consists of program code, data, Data + BSS
heap, stack and etc. 2KB
Heap
3KB
(free)
15KB
Stack
16KB
Address Space
Address Space
• Code 0KB
Program Code
– Where instructions live 1KB
Data + BSS
• Heap 2KB
Heap
– Dynamically allocate memory. 3KB
• malloc in C language
• new in object-oriented language (free)
• Stack
– Store return addresses or values.
15KB
– Contain local variables arguments Stack
to routines/functions/methods. 16KB
Address Space
Virtual Address
• Every address in a running process is virtual.
– OS translates the virtual address to physical address
#include <stdio.h>
#include <stdlib.h>
return x;
}
heap
(free)
stack
0x7ffd44000000
Stack
0x7ffd44c00000
MEMORY API
Memory API: malloc()
#include <stdlib.h>
40
Memory API: free()
#include <stdlib.h>
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
16KB
Address Space
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
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
Forgetting To Allocate Memory
• 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
*dst *dst
*src *src
Address Space Address Space
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
‘\0’ is omitted h
e
strlen l
6 bytes l
o
\0
5 bytes hello
\0
*dst
*src
Address Space
Forgetting to Initialize
• Encounter an uninitialized read
int *x = (int *)malloc(sizeof(int)); // allocated
printf(“*x = %d\n”, *x); // uninitialized memory access
heap heap
(free) (free)
stack stack
*x *x
Address Space Address Space
Memory Leak
• A program runs out of memory and eventually dies.
unused : unused, but not freed
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
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
Other Memory APIs: calloc()
#include <stdlib.h>
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
Other Memory APIs: realloc()
#include <stdlib.h>