Download presentation
Presentation is loading. Please wait.
1
. Memory Management
2
Memory Organization u During run time, variables can be stored in one of three “pools” Stack Static heap Dynamic heap
3
Stack u Maintains memory during function calls Argument of the function Local variables Call Frame u Variables on the stack have limited “life time”
4
Stack - Example int foo( int a, double f ) { int b; … } a f b
5
Stack - Example int foo( int a, double f ) { int b; … { int c; … } … } a f b
6
Stack - Example int foo( int a, double f ) { int b; … { int c; … } … } a f b c
7
Stack - Example int foo( int a, double f ) { int b; … { int c; … } … } a f b c
8
Stack - Example int foo( int a, double f ) { int b; … { int c; … } … } a f b c
9
Stack – recursive example void foo( int depth ) { int a; if( depth > 1 ) foo( depth-1 ); } int main() { foo(3); … depth a depth a depth a
10
Stack -- errors void foo( int depth ) { int a; if( depth > 1 ) foo( depth ); } Will result in run time error: out of stack space
11
Static heap u Memory for global variables #include const int ListOfNumbersSize = 1000; int ListOfNumbers[ListOfNumbersSize]; int main() { …
12
Static heap u Variables on the static heap are defined throughout the execution of the program u Memory on the static heap must be defined at compile time
13
Static heap: reverse example Example: program to reverse the order of lines of a file To this task, we need to u read the lines into memory u Print lines in reverse How do we store the lines in memory?
14
Static heap: reverse example const int LineLength = 100; const int NumberOfLines = 10000; char Lines[NumberOfLines][LineLength]; … int main() { int n = ReadLines(); for( n-- ; n >= 0; n-- ) printf(“%s\n”, Lines[n]); }
15
Static heap: reverse example This solution is problematic: u The program cannot handle files larger than these specified by the compile time choices If we set NumberOfLines to be very large, then the program requires this amount of memory even if we are reversing a short file Want to use memory on “as needed” basis
16
Dynamic Heap u Memory that can be allocated and freed by the program during run time u The program controls how much is allocated and when u Limitations based on run-time situation Available memory on the computer
17
Allocating Memory from Heap void *malloc( size_t Size ); Returns a pointer to a new memory block of size Size Returns NULL if it cannot allocate memory of this size
18
Example: strdup Function to duplicate a string: char * strdup( char const *p ) { int n = strlen(p); char* q =(char*)malloc(sizeof(char)*(n+1)); if( q != NULL ) strcpy( q, p ); return q; } This function is part of the standard library
19
Memory Management void foo( char const* p ) { char *q = strdup( p ); // do something with q } The allocated memory remains in use u cannot be reused later on p q … Heap ‘a’ ‘b’ ‘\0’
20
De-allocating memory void free( void *p ); Returns the memory block pointed by p to the pool of unused memory u No error checking! If p was not allocated by malloc, undefined behavior
21
Example of free void foo( char const* p ) { char *q = strdup( p ); // do something with q free(q); } This version frees the allocated memory
22
Further Knowledge Read manual page of u malloc u calloc u free
23
Interfaces u A definition of a set of functions that provide a coherent module (or library) Data structure (e.g., list, binary tree) User interface (e.g., drawing graphics) Communication (e.g., device driver)
24
Interface - modularity u Hide the details of implementing the module from its usage Specification – “what” Implementation – “how”
25
Interface – information hiding u Hide “private” information from outside The “outside” program should not be able to use internal variables of the module Crucial for modularity u Resource management Define who controls allocation of memory
26
Example interface - StrStack u A module that allows to maintain a stack of strings u Operations: Create new Push string Pop string IsEmpty [See attached StrStack.h]
27
Implementation of StrStack Decision #1: data structure u Linked list u Array (static? dynamic?) u Linked list of arrays u … We choose linked list for simplicity
28
Implementation of StrStack Decision #2: Resource allocation u Duplicated strings on stack or keep pointer to original? u If duplicate, who is responsible for freeing them? We choose not to duplicate --- leave this choice to user of module
29
Implementation of StrStack u See StrStack.c
30
Using StrStack int main() { char *Line; StrStack *Stack = StrStackNew(); while( (Line = ReadLine()) != NULL ) StrStackPush( Stack, Line ); while( (Line = StrStackPop(Stack)) != NULL ) { printf("%s\n", Line ); free( Line ); } return 0; }
31
Interface Principles Hide implementation details u Hide data structures u Don’t provide access to data structures that might be changed in alternative implementation u A “visible” detail cannot be later changed without changing code using the interface!
32
Interface Principles Use small set of “primitive” actions u Provide to maximize functionality with minimal set of operations u Do not provide unneeded functions “just because you can”
33
Interface Principles Don’t reach behind the back u Do not use global variables or unexpected side effects u Do not assume specific order of operations by the user Such assumptions suggest the set of primitives is wrong
34
Interface Principle Consistent Mechanisms u Do similar things in a similar way strcpy(dest, source) memcpy(dest, source)
35
Interface Principle Resource Management u Free resource at the same level it was allocated u Assumptions about resources
36
Debugging 101 1. “Define” the bug --- reproduce it 2. Use debugger 3. Don’t panic --- think! 4. Divide & Conquer
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.