I have a list class which holds a pHead pointer and a typical print function. I use another function (not in the list class) that takes the above list as its argument(non-ref type) and calls its print function.When i call this "another function" to print the the list, it also call the destructor of the list after printing. Is it supposed to be like that ? Can someone explain that to me?
P.s: sorry for the bad english :P

- 61
- 8
-
3We can't tell what's your problem unless you provide a [MCVE] demonstrating it. – user0042 Nov 25 '17 at 07:03
-
because when you are passing the value to function as non -ref .. function is localy creating the list for it self and when your function task is complete and the function list variable scope is over it is calling the destructor for that var – Ankur Jyoti Phukan Nov 25 '17 at 07:06
2 Answers
A destructor function is called automatically when the object goes out of scope:
- the function ends
- the program ends
- a block containing local variables ends
- a delete operator is called
In your case, the destructor is called because when you pass the object of your list class, it creates a copy of that object in the function, and when the function ends this newly created object goes out of scope and hence the destructor is called.
Edit : Expanding onto the answer ; There are three main types of memory
- automatic storage (stack)
- dynamic storage (heap)
static storage
- If you allocate an object in automatic storage the the object will be destroyed once the scope is terminated; for example
void foo() { MyClass myclass_instance; myclass_instance.doSomething(); }
in the above case when the function terminates myclass_instance is destroyed automatically.
Objects reside in dynamic memory (the heap). They are allocated with new and in order for the dstructor to be called, you need to call delete:
int main() { A* a = new A; delete a; //destructor called }
Static allocation : Objects reside in static memory. Regardless of where they are allocated, the destructor is automatically called when the program ends:
A a; //namespace scope int main() { }
Here, As destructor is called when the program terminates, after main finishes.

- 1,541
- 1
- 15
- 24
-
-
Maybe in the newer versions of the compiler, because I recall studying that it does. Because its also said, that put large memory objects in the heap with dynamic allocation. – Shivansh Jagga Nov 25 '17 at 08:00
-
Just search for the word _heap_ in [this document](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf). None of the occurrences has to do with memory allocation. – user0042 Nov 25 '17 at 08:05
-
https://stackoverflow.com/questions/6161235/what-is-the-difference-between-the-heap-and-the-free-store – Christian Hackl Nov 25 '17 at 10:13
-
Basically free store is the term used, and the data structure of free store is heap. Thanks for the new information:) – Shivansh Jagga Nov 25 '17 at 10:49
The destructor is called for an object if it reaches the end of its life span, i.e.
- When the control reaches the end of its scope (closing curly braces) (for objects with automatic storage duration)
- When you call
delete
(ordelete[]
) for objects allocated withnew
(ornew[]
) (for objects with dynamic storage duration) - When its parent is destroyed (for class member object)
- When you manually delete an object created with an overloaded
new
operator - When the program exits (for objects with static storage duration)
According to your explanation, I guess that you're passing it by value. When the function reaches its end, the copy of the object also reaches the end of its life span, so its destructor is called automatically.
For example (Case x
means object tx
is destroyed here):
struct T{
int a;
~T() { /* anything */ }
} t5;
struct U{
T t3;
};
void foo() {
T t1;
} // Case 1
int main() {
foo();
// Case 1
T *pt2 = new T();
delete pt2; // Case 2
{
U u1;
} // Case 3
void *buf = malloc(1000);
T *pt4 = new(buf) T();
pt4 -> ~T(); // Case 4
return 0; // Case 5
}

- 35,554
- 7
- 89
- 134