COMP 320 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

COMP 320

Description:

Mainly an old efficiency hack. 9. Allocation Summary. Dynamic size. Static size ... If data structure is used until program termination, don't need to free. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 25
Provided by: johngr7
Category:
Tags: comp | free | hacking | program

less

Transcript and Presenter's Notes

Title: COMP 320


1
COMP 320
  • Memory management basics
  • Allocation Deallocation

2
Readings
  • Read BO 3.8,3.11,3.13, 10.11.
  • Read HS 5.2-5.3.
  • Read VDL 10.

3
Allocation
  • For all data, memory must be allocated.
  • Allocated memory space reserved.
  • Arrays provide good examples for all the issues.
  • Two questions
  • When do we know how much to allocate?
  • When do we allocate?
  • Two possible answers for each
  • Compile-time (static)
  • Run-time (dynamic)

4
How Much to Allocate?
  • Static size specified by declaration
  • Dynamic size uses malloc()
  • Finds specified of bytes of unused space and
    reserves it.
  • Essentially equivalent to new in Java C.

char c int array10
include int array (int ) mall
oc(num_items sizeof(int))
5
Using malloc()
int i i (int ) malloc(sizeof(int))
i 3
int array array (int ) malloc(num_items
sizeof(int))
array3 5
Arrays ? pointers to the initial (0th) array
element.
6
Using malloc()
  • Always check the return value of system calls
    like malloc() for errors.
  • For brevity, wont in class.

int a (int ) malloc(num_items
sizeof(int)) if (a NULL) fprintf(stderr
,Out of memory.\n)
exit(1)
7
When to Allocate?
  • Static time
  • Typically global variables
  • Only one copy ever exists, so can allocate at
    compile-time.
  • Dynamic time
  • Typically local variables
  • One copy exists for each call. May be unbounded
    of calls, so cant allocate at compile-time.

int value int main()
int f() int value int main()


8
When to Allocate?
  • Static time
  • Some local variables
  • One copy exists for all calls. Allocated at
    compile-time.

int f() static int value int m
ain()

Not very common. Mainly an old efficiency hack.
Confusingly, local static has nothing to do with
global static!
9
Allocation Summary
Global declaration
Local non-static declaration
Local static declaration
Constant-size malloc()
Nonsense
Non-constant-size malloc()
10
Deallocation
  • Space allocated via declaration (entering scope)
    is deallocated when exiting scope.
  • Cant refer to y or array outside of f(), so
    their space is deallocated upon return.

f() int y int array10
11
Deallocation
  • Space allocated by malloc() is deallocated by
    free().
  • Not automatically deallocated (garbage collected)
    as in Scheme Java.
  • Forgetting to deallocate leads to memory leaks
    running out of memory.
  • Must not use a freed pointer unless reassigned or
    reallocated.

int a (int ) malloc(num_items
sizeof(int)) free(a) a (int ) malloc(
2 num_items sizeof(int))
12
Deallocation
  • Space allocated by malloc() is freed when the
    program terminates.
  • If data structure is used until program
    termination, dont need to free.
  • Entire process memory is deallocated.
  • Assumes virtual memory implementation is
    bug-free!
  • Bad assumption.

13
Common Memory Management Mistakes
14
Whats Wrong With This Code?
int make_array() int array10
return array
int f() int i return i
  • Consider j f().
  • Leads to referencing deallocated memory.
  • Never return a pointer to a local variable!
  • Behavior depends on allocation pattern.
  • Space not reallocated ? works.
  • Space reallocated ? very unpredictable.

15
One Solution
int f() int i_ptr (int ) m
alloc(sizeof(int))
return i_ptr
int make_array() int array
(int ) malloc(10 sizeof(int))

return array
  • Allocate with malloc(), and return its pointer.
  • Upon return, space for local pointer variable is
    deallocated.
  • But the malloc-ed space isnt deallocated until
    it is free-d.

16
Whats Wrong With This Code?
/ return y Ax / int matvec(int A, int
x) int y (int ) malloc(N sizeof(in
t)) int i, j for ( i for ( jj return y
17
Whats Wrong With This Code?
char p int i / Allocate space for
MN matrix / p malloc(M sizeof(char)) fo
r (i0 iar))
18
Whats Wrong With This Code?
char p int i / Allocate space for
MN matrix / p (char ) malloc(M sizeof(cha
r )) for (i0 i) malloc(N sizeof(char))
19
Whats Wrong With This Code?
char s 1234567 char t7 strcpy(t
, s)
20
Whats Wrong With This Code?
int binheap_delete(int binheap, int
size) int packet packet binheap
0 binheap0 binheapsize - 1 size
- 1 heapify(binheap, size, 0) return(p
acket)
21
Whats Wrong With This Code?
/ Search memory for a value. /
/ Assume value is present. /
int search(int p, int value)
while (p 0 p ! value)
p sizeof(int) return p
22
Whats Wrong With This Code?
x (int ) malloc(N sizeof(int))
free(x) y (int ) malloc(M sizeof(int)
) for (i0 ii 1
23
Whats Wrong With This Code?
void foo() int x (int ) malloc(N siz
eof(int))
return
24
Whats Wrong With This Code?
struct ACons int first stru
ct ACons rest typedef struct ACons Li
st void foo() List list cons(1,cons(2
,cons(3,NULL))) free(list) return

A peek at how well define lists.
Write a Comment
User Comments (0)
About PowerShow.com