EECS 280 Lecture Notes - Lecture 23: Memory Leak, Memory Management
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.