6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to...
Transcript of 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to...
![Page 1: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/1.jpg)
6.096 Lecture 8: Memory Management
Clean up after your pet program
Geza Kovacs
![Page 2: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/2.jpg)
Review: Constructors • Method that is called when an instance is created
class Integer { public: int val; Integer() { val = 0; cout << "default constructor" << endl; } }; int main() { Integer i; }
Output: default constructor
![Page 3: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/3.jpg)
• When making an array of objects, default constructor is invoked on each
class Integer { public: int val; Integer() { val = 0; cout << "default constructor" << endl; } }; int main() { Integer arr[3]; }
Output: default constructor default constructor default constructor
![Page 4: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/4.jpg)
• When making a class instance, the default constructor of its fields are invoked
class Integer { public: int val; Integer() { val = 0; cout << "Integer default constructor" << endl; } }; class IntegerWrapper { public: Integer val; IntegerWrapper() { cout << "IntegerWrapper default constructor" << endl; } }; int main() { IntegerWrapper q; }
Output: Integer default constructor IntegerWrapper default constructor
![Page 5: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/5.jpg)
• Constructors can accept parameters
class Integer { public: int val; Integer(int v) { val = v; cout << "constructor with arg " << v << endl; } }; int main() { Integer i(3); }
Output: constructor with arg 3
![Page 6: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/6.jpg)
• Constructors can accept parameters
– Can invoke single-parameter constructor via assignment to the appropriate type
class Integer { public: int val; Integer(int v) { val = v; cout << "constructor with arg " << v << endl; } }; int main() { Integer i(3); Integer j = 5; }
Output: constructor with arg 3 constructor with arg 5
![Page 7: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/7.jpg)
class Integer { public: int val; Integer(int v) { val = v; } }; int main() { Integer i(3); // ok Integer j; }
• If a constructor with parameters is defined, the default constructor is no longer available
Error: No default constructor available for Integer
![Page 8: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/8.jpg)
class Integer { public: int val; Integer(int v) { val = v; } }; int main() { Integer a[] = { Integer(2), Integer(5) }; // ok Integer b[2]; }
• If a constructor with parameters is defined, the default constructor is no longer available
– Without a default constructor, can’t declare arrays without initializing
Error: No default constructor available for Integer
![Page 9: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/9.jpg)
class Integer { public: int val; Integer() { val = 0; } Integer(int v) { val = v; } }; int main() { Integer i; // ok Integer j(3); // ok }
• If a constructor with parameters is defined, the default constructor is no longer available
– Can create a separate 0-argument constructor
![Page 10: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/10.jpg)
class Integer { public: int val; Integer(int v = 0) { val = v; } }; int main() { Integer i; // ok Integer j(3); // ok }
• If a constructor with parameters is defined, the default constructor is no longer available
– Can create a separate 0-argument constructor
– Or, use default arguments
![Page 11: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/11.jpg)
class Integer { public: int val; Integer(int val = 0) { this->val = val; } };
• How do I refer to a field when a method argument has the same name?
• this: a pointer to the current instance
this->val is a shorthand for (*this).val
![Page 12: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/12.jpg)
class Integer { public: int val; Integer(int val = 0) { this->val = val; } void setVal(int val) { this->val = val; } };
• How do I refer to a field when a method argument has the same name?
• this: a pointer to the current instance
![Page 13: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/13.jpg)
Scoping and Memory
• Whenever we declare a new variable (int x), memory is allocated
• When can this memory be freed up (so it can be used to store other variables)?
– When the variable goes out of scope
![Page 14: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/14.jpg)
int main() { if (true) { int x = 5; } // x now out of scope, memory it used to occupy can be reused }
• When a variable goes out of scope, that memory is no longer guaranteed to store the variable’s value
Scoping and Memory
![Page 15: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/15.jpg)
int main() { int *p; if (true) { int x = 5; p = &x; } cout << *p << endl; // ??? }
• When a variable goes out of scope, that memory is no longer guaranteed to store the variable’s value
Scoping and Memory
![Page 16: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/16.jpg)
int main() { int *p; if (true) { int x = 5; p = &x; } cout << *p << endl; // ??? }
int *p
here
• When a variable goes out of scope, that memory is no longer guaranteed to store the variable’s value
Scoping and Memory
![Page 17: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/17.jpg)
int *p
int xint main() { int *p; if (true) { int x = 5; p = &x; } cout << *p << endl; // ??? }
here
• When a variable goes out of scope, that memory is no longer guaranteed to store the variable’s value
Scoping and Memory
![Page 18: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/18.jpg)
int *p
int xint main() { int *p; if (true) { int x = 5; p = &x; } cout << *p << endl; // ??? }
here
• When a variable goes out of scope, that memory is no longer guaranteed to store the variable’s value
Scoping and Memory
![Page 19: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/19.jpg)
int main() { int *p; if (true) { int x = 5; p = &x; } cout << *p << endl; // ??? }
here
int *p
???
• When a variable goes out of scope, that memory is no longer guaranteed to store the variable’s value
– Here, p has become a dangling pointer (points to memory whose contents are undefined)
Scoping and Memory
![Page 20: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/20.jpg)
A Problematic Task
• Implement a function which returns a pointer to some memory containing the integer 5
• Incorrect implementation:
int* getPtrToFive() { int x = 5; return &v; }
![Page 21: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/21.jpg)
• Implement a function which returns a pointer to some memory containing the integer 5
• Incorrect implementation:
– x is declared in the function scope
int* getPtrToFive() { int x = 5; return &x; } int main() { int *p = getPtrToFive(); cout << *p << endl; // ??? }
here int x
![Page 22: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/22.jpg)
• Implement a function which returns a pointer to some memory containing the integer 5
• Incorrect implementation:
– x is declared in the function scope
– As getPtrToFive() returns, x goes out of scope. So a dangling pointer is returned
int* getPtrToFive() { int x = 5; return &x; } int main() { int *p = getPtrToFive(); cout << *p << endl; // ??? }
here
int *p
???
![Page 23: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/23.jpg)
The new operator
• Another way to allocate memory, where the memory will remain allocated until you manually de-allocate it
• Returns a pointer to the newly allocated memory
int *x = new int;
![Page 24: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/24.jpg)
The new operator
• Another way to allocate memory, where the memory will remain allocated until you manually de-allocate it
• Returns a pointer to the newly allocated memory
int *x = new int;
Type parameter needed to determine how much memory to allocate
![Page 25: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/25.jpg)
The new operator
• Another way to allocate memory, where the memory will remain allocated until you manually de-allocate it
• Returns a pointer to the newly allocated memory
• Terminology note:
– If using int x; the allocation occurs on a region of memory called the stack
– If using new int; the allocation occurs on a region of memory called the heap
![Page 26: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/26.jpg)
The delete operator
• De-allocates memory that was previously allocated using new
• Takes a pointer to the memory location
int *x = new int; // use memory allocated by new delete x;
![Page 27: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/27.jpg)
• Implement a function which returns a pointer to some memory containing the integer 5
– Allocate memory using new to ensure it remains allocated
int *getPtrToFive() { int *x = new int; *x = 5; return x; }
![Page 28: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/28.jpg)
• Implement a function which returns a pointer to some memory containing the integer 5
– Allocate memory using new to ensure it remains allocated.
– When done, de-allocate the memory using delete
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p = getPtrToFive(); cout << *p << endl; // 5 delete p; }
![Page 29: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/29.jpg)
Delete Memory When Done Using It
• If you don’t use de-allocate memory using delete, your application will waste memory
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; } }
incorrect
![Page 30: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/30.jpg)
• If you don’t use de-allocate memory using delete, your application will waste memory
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; } }
int *p
![Page 31: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/31.jpg)
The Heap
int *p
5
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; } }
1st iteration
• If you don’t use de-allocate memory using delete, your application will waste memory
![Page 32: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/32.jpg)
The Heap
int *p
5 5
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; } }
2nd iteration
• If you don’t use de-allocate memory using delete, your application will waste memory
![Page 33: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/33.jpg)
The Heap
int *p
5 5 5
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; } }
3rd iteration
• If you don’t use de-allocate memory using delete, your application will waste memory
• When your program allocates memory but is unable to de-allocate it, this is a memory leak
![Page 34: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/34.jpg)
The Heap
int *p
5 5 5
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; }
delete p; }
3rd iteration
• Does adding a delete after the loop fix this memory leak?
![Page 35: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/35.jpg)
The Heap
int *p
5 5
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; }
delete p; }
• Does adding a delete after the loop fix this memory leak?
– No; only the memory that was allocated on the last iteration gets de-allocated
![Page 36: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/36.jpg)
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; delete p; } }
• To fix the memory leak, de-allocate memory within the loop
![Page 37: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/37.jpg)
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; delete p; } }
• To fix the memory leak, de-allocate memory within the loop
int *p
![Page 38: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/38.jpg)
The Heap
int *p
5
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; delete p; } }
• To fix the memory leak, de-allocate memory within the loop
1st iteration
![Page 39: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/39.jpg)
The Heap
int *p
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; delete p; } }
• To fix the memory leak, de-allocate memory within the loop
1st iteration
![Page 40: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/40.jpg)
The Heap
int *p
5
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; delete p; } }
• To fix the memory leak, de-allocate memory within the loop
2nd iteration
![Page 41: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/41.jpg)
The Heap
int *p
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; delete p; } }
• To fix the memory leak, de-allocate memory within the loop
2nd iteration
![Page 42: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/42.jpg)
The Heap
int *p
5
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; delete p; } }
• To fix the memory leak, de-allocate memory within the loop
3rd iteration
![Page 43: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/43.jpg)
The Heap
int *p
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *p; for (int i = 0; i < 3; ++i) { p = getPtrToFive(); cout << *p << endl; delete p; } }
• To fix the memory leak, de-allocate memory within the loop
3rd iteration
![Page 44: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/44.jpg)
Don’t Use Memory After Deletion
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *x = getPtrToFive(); delete x; cout << *x << endl; // ??? }
incorrect
![Page 45: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/45.jpg)
Don’t Use Memory After Deletion
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *x = getPtrToFive(); delete x; cout << *x << endl; // ??? }
incorrect
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *x = getPtrToFive(); cout << *x << endl; // 5 delete x; }
correct
![Page 46: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/46.jpg)
Don’t delete memory twice
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *x = getPtrToFive(); cout << *x << endl; // 5 delete x; delete x; }
incorrect
![Page 47: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/47.jpg)
Don’t delete memory twice
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *x = getPtrToFive(); cout << *x << endl; // 5 delete x; delete x; }
incorrect
int *getPtrToFive() { int *x = new int; *x = 5; return x; } int main() { int *x = getPtrToFive(); cout << *x << endl; // 5 delete x; }
correct
![Page 48: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/48.jpg)
Only delete if memory was allocated by new
int main() { int x = 5; int *xPtr = &x; cout << *xPtr << endl; delete xPtr; }
incorrect
![Page 49: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/49.jpg)
Only delete if memory was allocated by new
int main() { int x = 5; int *xPtr = &x; cout << *xPtr << endl; delete xPtr; }
incorrect
int main() { int x = 5; int *xPtr = &x; cout << *xPtr << endl; }
correct
![Page 50: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/50.jpg)
int numItems; cout << "how many items?"; cin >> numItems; int arr[numItems]; // not allowed
Allocating Arrays
• When allocating arrays on the stack (using “int arr[SIZE]”), size must be a constant
![Page 51: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/51.jpg)
Allocating Arrays
• If we use new[] to allocate arrays, they can have variable size
int numItems; cout << "how many items?"; cin >> numItems; int *arr = new int[numItems];
Type of items in array
![Page 52: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/52.jpg)
Allocating Arrays
• If we use new[] to allocate arrays, they can have variable size
int numItems; cout << "how many items?"; cin >> numItems; int *arr = new int[numItems];
Number of items to allocate
![Page 53: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/53.jpg)
Allocating Arrays
• If we use new[] to allocate arrays, they can have variable size
• De-allocate arrays with delete[]
int numItems; cout << "how many items?"; cin >> numItems; int *arr = new int[numItems]; delete[] arr;
![Page 54: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/54.jpg)
Ex: Storing values input by the user
int main() { int numItems; cout << "how many items? "; cin >> numItems; int *arr = new int[numItems]; for (int i = 0; i < numItems; ++i) { cout << "enter item " << i << ": "; cin >> arr[i]; } for (int i = 0; i < numItems; ++i) { cout << arr[i] << endl; } delete[] arr; }
how many items? 3 enter item 0: 7 enter item 1: 4 enter item 2: 9 7 4 9
![Page 55: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/55.jpg)
Allocating Class Instances using new
• new can also be used to allocate a class instance
class Point { public: int x, y; }; int main() { Point *p = new Point; delete p; }
![Page 56: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/56.jpg)
Allocating Class Instances using new
• new can also be used to allocate a class instance
• The appropriate constructor will be invoked class Point { public: int x, y; Point() { x = 0; y = 0; cout << "default constructor" << endl; } }; int main() { Point *p = new Point; delete p; }
Output: default constructor
![Page 57: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/57.jpg)
Allocating Class Instances using new
• new can also be used to allocate a class instance
• The appropriate constructor will be invoked class Point { public: int x, y; Point(int nx, int ny) { x = ny; x = ny; cout << "2-arg constructor" << endl; } }; int main() { Point *p = new Point(2, 4); delete p; }
Output: 2-arg constructor
![Page 58: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/58.jpg)
Destructor
• Destructor is called when the class instance gets de-allocated
class Point { public: int x, y; Point() { cout << "constructor invoked" << endl; } ~Point() { cout << "destructor invoked" << endl; } }
![Page 59: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/59.jpg)
• Destructor is called when the class instance gets de-allocated
– If allocated with new, when delete is called
class Point { public: int x, y; Point() { cout << "constructor invoked" << endl; } ~Point() { cout << "destructor invoked" << endl; } }; int main() { Point *p = new Point; delete p; }
Output: constructor invoked destructor invoked
![Page 60: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/60.jpg)
• Destructor is called when the class instance gets de-allocated
– If allocated with new, when delete is called
– If stack-allocated, when it goes out of scope
class Point { public: int x, y; Point() { cout << "constructor invoked" << endl; } ~Point() { cout << "destructor invoked" << endl; } }; int main() { if (true) { Point p; } cout << "p out of scope" << endl; }
Output: constructor invoked destructor invoked p out of scope
![Page 61: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/61.jpg)
Representing an Array of Integers
• When representing an array, often pass around both the pointer to the first element and the number of elements
– Let’s make them fields in a class
class IntegerArray { public: int *data; int size; };
Pointer to the first element
![Page 62: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/62.jpg)
Representing an Array of Integers
• When representing an array, often pass around both the pointer to the first element and the number of elements
– Let’s make them fields in a class
class IntegerArray { public: int *data; int size; };
Number of elements in the array
![Page 63: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/63.jpg)
class IntegerArray { public: int *data; int size; }; int main() { IntegerArray arr; arr.size = 2; arr.data = new int[arr.size]; arr.data[0] = 4; arr.data[1] = 5; delete[] a.data; }
![Page 64: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/64.jpg)
class IntegerArray { public: int *data; int size; }; int main() { IntegerArray arr; arr.size = 2; arr.data = new int[arr.size]; arr.data[0] = 4; arr.data[1] = 5; delete[] a.data; }
Can move this into a constructor
![Page 65: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/65.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } }; int main() { IntegerArray arr(2); arr.data[0] = 4; arr.data[1] = 5; delete[] arr.data; }
![Page 66: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/66.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } }; int main() { IntegerArray arr(2); arr.data[0] = 4; arr.data[1] = 5; delete[] arr.data; }
Can move this into a destructor
![Page 67: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/67.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } ~IntegerArray () { delete[] data; } }; int main() { IntegerArray arr(2); arr.data[0] = 4; arr.data[1] = 5; }
De-allocate memory used by fields in destructor
![Page 68: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/68.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } ~IntegerArray() { delete[] data; } }; int main() { IntegerArray a(2); a.data[0] = 4; a.data[1] = 2; if (true) { IntegerArray b = a; } cout << a.data[0] << endl; // not 4! }
incorrect
![Page 69: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/69.jpg)
a (IntArrayWrapper)
4 2
data
here
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } ~IntegerArray() { delete[] data; } }; int main() { IntegerArray a(2); a.data[0] = 4; a.data[1] = 2; if (true) { IntegerArray b = a; } cout << a.data[0] << endl; // not 4! }
![Page 70: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/70.jpg)
a (IntArrayWrapper) b (IntArrayWrapper)
4 2
data data
here
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } ~IntegerArray() { delete[] data; } }; int main() { IntegerArray a(2); a.data[0] = 4; a.data[1] = 2; if (true) { IntegerArray b = a; } cout << a.data[0] << endl; // not 4! }
• Default copy constructor copies fields
![Page 71: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/71.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } ~IntegerArray() { delete[] data; } }; int main() { IntegerArray a(2); a.data[0] = 4; a.data[1] = 2; if (true) { IntegerArray b = a; } cout << a.data[0] << endl; // not 4! }
• When b goes out of scope, destructor is called (de-allocates array), a.data now a dangling pointer
a (IntArrayWrapper)
data
(Deleted)
here
![Page 72: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/72.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } ~IntegerArray() { delete[] data; } }; int main() { IntegerArray a(2); a.data[0] = 4; a.data[1] = 2; if (true) { IntegerArray b = a; } cout << a.data[0] << endl; // not 4! }
• 2nd bug: when a goes out of scope, its destructor tries to delete the (already-deleted) array
a (IntArrayWrapper)
data
(Deleted)
Program crashes as it terminates
![Page 73: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/73.jpg)
• Write your own a copy constructor to fix these bugs
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } IntegerArray(IntegerArray &o) { data = new int[o.size]; size = o.size; for (int i = 0; i < size; ++i) data[i] = o.data[i]; } ~IntegerArray() { delete[] data; } };
![Page 74: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/74.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } IntegerArray(IntegerArray &o) { data = new int[o.size]; size = o.size; for (int i = 0; i < size; ++i) data[i] = o.data[i]; } ~IntegerArray() { delete[] data; } }; int main() { IntegerArray a(2); a.data[0] = 4; a.data[1] = 2; if (true) { IntegerArray b = a; } cout << a.data[0] << endl; // 4 }
here
a (IntArrayWrapper)
4 2
data
![Page 75: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/75.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } IntegerArray(IntegerArray &o) { data = new int[o.size]; size = o.size; for (int i = 0; i < size; ++i) data[i] = o.data[i]; } ~IntegerArray() { delete[] data; } }; int main() { IntegerArray a(2); a.data[0] = 4; a.data[1] = 2; if (true) { IntegerArray b = a; } cout << a.data[0] << endl; // 4 }
Copy constructor invoked
a (IntArrayWrapper) b (IntArrayWrapper)
4 2
data data
4 2
![Page 76: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/76.jpg)
class IntegerArray { public: int *data; int size; IntegerArray(int size) { data = new int[size]; this->size = size; } IntegerArray(IntegerArray &o) { data = new int[o.size]; size = o.size; for (int i = 0; i < size; ++i) data[i] = o.data[i]; } ~IntegerArray() { delete[] data; } }; int main() { IntegerArray a(2); a.data[0] = 4; a.data[1] = 2; if (true) { IntegerArray b = a; } cout << a.data[0] << endl; // 4 }
here
a (IntArrayWrapper)
4 2
data
![Page 77: 6.096 Lecture 8: Memory Management - Karlstad University · The new operator •Another way to allocate memory, where the memory will remain allocated until you manually de-allocate](https://reader034.fdocuments.us/reader034/viewer/2022042313/5edca3f2ad6a402d666764a4/html5/thumbnails/77.jpg)
MIT OpenCourseWarehttp://ocw.mit.edu
6.096 Introduction to C++January (IAP) 2011
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.