EECS 280 Lecture Notes - Lecture 23: Memory Leak, Memory Management

62 views3 pages

Document Summary

Containers use templates, so they can hold any type. Including custom ties with the class mechanism class gorilla { string name; string get_name(); Gorilla(string name_in) : name(name_in) { cout << name << endl; int main() { Instead of passing by value, pass by reference! Passing by value makes another copy with takes up memory and makes the program slower. void forward_list:: push_front(const t &datum) { //fix this by adding & - pass by reference. Node *p = new node; p->datum = datum; //this line still makes a copy though! p->next = front; front = p; Forward_list zoo; //make a list of pointers to custom type zoo. push_front(new gorilla( colo ); //store a pointer to a custom type value in the container. In this code, the only thing that runs is the gorilla custom actor, but we"re making the pointers on the heap. In the end, the local variables go away, so the zoo goes away.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents