01 Dynamic Memory Allocation


University of Washington
Memory & data
Roadmap
Integers & floats
Machine code & C
C:
Java:
x86 assembly
Car c = new Car();
car *c = malloc(sizeof(car));
Procedures & stacks
c.setMiles(100);
c->miles = 100;
Arrays & structs
c.setGals(17);
c->gals = 17;
Memory & caches
float mpg =
float mpg = get_mpg(c);
Processes
c.getMPG();
free(c);
Virtual memory
Memory alloca on
get_mpg:
Assembly
pushq %rbp
Java vs. C
language:
movq %rsp, %rbp
...
popq %rbp
ret
OS:
0111010000011000
Machine
100011010000010000000010
code:
1000100111000010
110000011111101000011111
Computer
system:
Memory Alloca on
University of Washington
Sec on 10: Memory Alloca on Topics
Dynamic memory alloca on
Size/number of data structures may only be known at run me
Need to allocate space on the heap
Need to de allocate (free) unused memory so it can be re allocated
Implementa on
Implicit free lists
Explicit free lists  subject of next programming assignment
Segregated free lists
Garbage collec on
Common memory related bugs in C programs
Memory Alloca on
University of Washington
Dynamic Memory Alloca on
Applica on
Programmers use
dynamic memory
Dynamic Memory Allocator
allocators (such as
Heap
malloc) to acquire
memory at run me.
For data structures whose
User stack
size is only known at
run me.
Top of heap
Dynamic memory
(brk ptr)
Heap (via malloc)
allocators manage an
Unini alized data (.bss)
area of process virtual
Ini alized data (.data)
memory known as the
Program text (.text)
heap.
0
Memory Alloca on
University of Washington
Dynamic Memory Alloca on
Allocator maintains heap as collec on of variable sized
blocks, which are either allocated or free
Allocator requests space in heap region; VM hardware and kernel
allocate these pages to the process
Applica on objects are typically smaller than pages, so the allocator
manages blocks within pages
Types of allocators
Explicit allocator: applica on allocates and frees space
E.g. malloc and free in C
Implicit allocator: applica on allocates, but does not free space
E.g. garbage collec on in Java, ML, and Lisp
Memory Alloca on
University of Washington
The malloc Package
#include
void *malloc(size_t size)
Successful:
Returns a pointer to a memory block of at least size bytes
(typically) aligned to 8 byte boundary
If size == 0, returns NULL
Unsuccessful: returns NULL and sets errno
void free(void *p)
Returns the block pointed at by p to pool of available memory
p must come from a previous call to malloc or realloc
Other func ons
calloc: Version of malloc that ini alizes allocated block to zero.
realloc: Changes the size of a previously allocated block.
sbrk: Used internally by allocators to grow or shrink the heap.
Memory Alloca on
University of Washington
Malloc Example
void foo(int n, int m) {
int i, *p;
/* allocate a block of n ints */
p = (int *)malloc(n * sizeof(int));
if (p == NULL) {
perror("malloc");
exit(0);
}
for (i=0; i /* add space for m ints to end of p block */
if ((p = (int *)realloc(p, (n+m) * sizeof(int))) == NULL) {
perror("realloc");
exit(0);
}
for (i=n; i < n+m; i++) p[i] = i;
/* print new array */
for (i=0; i printf("%d\n", p[i]);
free(p); /* return p to available memory pool */
}
Memory Alloca on


Wyszukiwarka

Podobne podstrony:
01 Dynamic Memory Allocation
01 Virtual Memory Overview
chapter 2 memory allocation
01 Stacks in Memory and Stack Operations
01 Array AllocationAccesses
01 Introduction to Chassis Dynamics
01 Array AllocationAccesses
01 Array AllocationAccesses
01 Ajdukiewicz A i inni Zagrozenia jakosci?tonu w konstrukcji wskutek oddzialywan dynamicznych w sas
A03 Dynamika (01 05)
Bifurkacje, Chaos i Fraktale w Dynamice Wahadla 01 Szemplinska p32
01 11 Erase DTC s memory end output
t informatyk12[01] 02 101
r11 01
2570 01
introligators4[02] z2 01 n
Biuletyn 01 12 2014

więcej podobnych podstron