Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Memory.

Similar presentations


Presentation on theme: "Dynamic Memory."— Presentation transcript:

1 Dynamic Memory

2 Objective The efficient use of Memory Use no more than is needed at any given time.

3 Classifications of Memory
1. Static 2. Automatic 3. Dynamic Classified by: - When objects are allocated and deallocated - How objects are allocated and deallocated - The Memory Section on which objects reside

4 Static Memory Allocated: before main() begins Deallocated: after main() ends Section: Data Segment Example: Global Variables

5 Automatic Memory Allocated: when the declaration statement is executed inside a block Deallocated: at the end of the block in which it is declared Section: Stack Examples: Local Variables, Formal Arguments

6 Dynamic Memory Allocated: on demand by the programmer using the new operator. Deallocated: on demand by the programmer using the delete operator Section: Heap Examples: See below!

7 new operator Purpose: To allocate a data object "on demand" Syntax: pointer = new datatype; Semantics: 1. allocates an object of the datatype 2. points pointer to the new object

8 new operator Example 1: class frac { public: int num; int den;} void main() { frac * p; // p is automatic p = new frac; // new frac is dynamic p->num = 2; p->den = 3; }

9 new operator Example 1: class frac { public: int num; int den; } void main() { frac * p; p = new frac; p->num = 2; p->den = 3; }

10 new operator Example 1: class frac { public: int num; int den; } void main() { frac * p; p = new frac; p->num = 2; p->den = 3; } Note the object does not have its own name!

11 new operator Example 1: class frac { int num; int den; } void main() { frac * p; p = new frac; p->num = 2; p->den = 3; }

12 new operator Example 1: class frac { public: int num; int den; } void main() { frac * p; p = new frac; p->num = 2; p->den = 3; }

13 new[] operator Purpose: To allocate an array of data objects "on demand" Syntax: pointer = new datatype[intValue ]; The may be a variable, constant or literal. Once allocated, use as any Array. Semantics: same as new

14 new[] operator Example 2: void main() { int *p, num, i; cout << "How many numbers? "; cin >> num; p = new int[num]; for (i=0; i<n; i++) p[i] = 0; }

15 delete operator Purpose: To deallocate a data object "on demand" Syntax: delete pointer; Semantics: deallocates the data object pointed to by p. Does not set p to NULL!

16 delete operator Example 3: void main() { frac *p = new frac; ... delete p; // p is NOT set to NULL! p = NULL; // to be safe }

17 delete[] operator Purpose: To deallocate an array of data objects "on demand" Syntax: delete[] pointer; Semantics: deallocates the entire array of data objects pointed to by p. Does not set p to NULL!

18 delete[] operator Example 4: void main() { int *p = new int[5]; ... delete[] p; // deallocate all 5 ints p = NULL; // to be safe }

19 Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

20 Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

21 Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

22 Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

23 Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

24 Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

25 Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

26 Example 5 frac * createFrac(int n, int d) { frac * p = new frac; p->num = n; p->den = d; return p; } void main() { frac *f1, *f2; f1 = createFrac(2,3); f2 = createFrac(3,4); cout << f1->num << "/" << f1->den << endl; cout << f2->num << "/" << f2->den << endl;

27 Garbage A dynamically allocated memory object that has no pointer pointing to it. It cannot be referenced, so it cannot be used and cannot be deallocated, but still exists in memory (wasting space). Also called a Memory Leak Don't make Garbage!!

28 Garbage Example 5: void main() { int *p, i=4; p = new int; *p = 6; p = &i; }

29 Dangling Reference A pointer to a data object that no longer exists Attempting to use a Dangling Reference will cause a run time error. Don't create Dangling References!!

30 Dangling Reference Example 6: void main() { int *p, *q; p = new int; q = p; *p = 6; delete q; q = NULL; cout << *p; // CRASH! }

31 Vocabulary Term Definition Static
Memory that is allocated on the Data Segment before the program begins and deallocated after it ends. Ex: Global Variables Automatic Memory that is allocated on the Stack when the data object is declared and is deallocated at the end of the block in which it is declared. Dynamic Memory that is allocated on the Heap on-demand by the programmer (using the new operator), and deallocated on-demand by the programmer (using the delete operator) Garbage Dynamically-allocated data object that has no pointer pointing to it, and so can not be used or deleted. Dangling Reference A pointer to a data object that no longer exists (has been deleted).


Download ppt "Dynamic Memory."

Similar presentations


Ads by Google