Reputation: 3405
If I declare a struct such as this one:
struct MyStruct
{
int* nums;
};
MyStruct ms;
ms.nums = new int[4];
Do I need to call delete ms.nums;
before exiting my program or will the member variable nums
automatically get deallocated because the MyStruct instance ms
wasn't declared on the heap?
Upvotes: 2
Views: 3174
Reputation: 254431
Memory for the pointer itself will be deallocated when the object is destroyed, as it is for any class member. However, the array it points to won't be deleted - there is no way to know what the pointer points to, whether it was allocated by new
or new[]
or not dynamically allocated at all, or whether anything else still wants to use it. To avoid memory leaks, you should delete it, with the array form delete [] ms.nums;
, once you've finished with it.
Since it's often hard to do this correctly, why not use a library class to manage the array correctly for you?
#include <vector>
struct MyStruct
{
std::vector<int> nums;
};
MyStruct ms;
ms.nums.resize(4);
Now all the memory will be released automatically when ms
is destroyed; and the class is correctly copyable and (since C++11) efficiently movable.
Upvotes: 1
Reputation: 5039
When program exit, OS will free all memory allocated by your program. On the other hand, if a memory allocation occurs multiple times during the life of the program, then surely it should be released to prevent dangling pointers.
Upvotes: 0
Reputation: 2689
The struct
is allocated on the stack, which basically is a pointer to an Array
. the array is allocated on the heap
. Every time you write "new" that means that the allocation is on the heap therefore you need to "delete"
it. On the other hand the struct will be remove as soon as you exit the function.
Upvotes: 0
Reputation: 851
Yes, you have to delete that.. struct's default destructor won't do that. It will just delete the pointer variable which holds the address of the object and object will be left alone forever.
Better to do the deletion inside the struct's destructor
struct MyStruct
{
int* nums;
public :
~MyStruct()
{
if(nums != NULL)
delete nums;
}
};
Upvotes: 5
Reputation: 15377
The member variable nums
(which is a pointer) will get automatically deallocated when ms
, the struct containing it, is popped off the stack.
However, the memory pointed to by nums
will NOT. You should call delete [] ms.nums;
before your program exits.
Upvotes: 4
Reputation: 3992
You need to call delete
if memory allocated and pointer returned to num
. Here num
is a pointer inside a structure. Memory for num
is allocated when you declare structure variable. But memory address which is allocated by new
should be un-allocated by calling delete
Upvotes: 1
Reputation: 6116
You always have to call delete
to deallocate all the memory that you explicitly allocated using new
.
When you allocate memory using new
, memory is allocated in heap and the base address of the block is returned back.
Your struct
is present in stack which contains a pointer pointing to that base address. When the struct
object leaves the scope, the object is deallocated , i.e. the pointer containig the address of allocated heap memory block is gone, but the heap memory itself is still allocated which you can't deallocate now.
Upvotes: 2