Reputation: 35
I am new to C++ so I am struggling alot with memory management.
I know that when we have a pointer to an array, it means it is dynamically created, so I should delete it later in my destructor. But what if we have a normal array, do I need to include a delete statement in the destructor for this normal array too? or the program will do that automatically?
for example, I have a normal array like this
int myArray[];
and in my destructor, should I include this:
delete[] myArray;
?
Thanks in advance,
Upvotes: 0
Views: 1409
Reputation: 35
What I got from the answers is that I don't need to delete
anything if new
is not triggered. Same goes to delete[]
and new[]
. Normally created arrays like the one in my question int myArray[]
is not dynamically created, so it does not need to be dynamically deleted in destructor, meaning, the compiler will do the deleting part for me. Thanks all
Upvotes: 0
Reputation: 23
Memory management isn't easy for anyone, especially to those who are new to C++. I recommend looking into the terms stack
and heap
, which should hopefully clarify some things up.
I know that when we have a pointer to an array, it means it is dynamically created, so I should delete it later in my destructor.
This isn't exactly true, a pointer is just a variable that holds the address (where a variable lives) of something. You can make a pointer to any variable like this:
int a = 5;
int *aPtr = &a;
This doesn't mean that a
or aPtr
are dynamic.
However a pointer made like this would be dynamic and needs to be deleted:
int *aPtr = new int(5);
The biggest difference here is the new
keyword. new
makes dynamic memory (what is put onto the heap). Every time you use the new
keyword, you should use the delete
keyword to match with it.
But what if we have a normal array, do I need to include a delete statement in the destructor for this normal array too? or the program will do that automatically? for example, I have a normal array like this
int myArray[];
and in my destructor, should I include this:delete[] myArray;
?
No, you wouldn't need to call delete[] myArray;
because this array would be made on the stack. What I mean by that is that when you return out of the function that made this array, the array would be cleaned out of memory.
An alternative to manually managing memory is to use the std::array
, std::vector
, or even smart pointers such as std::unique_ptr
or std::shared_ptr
that clean themselves up after the reference to it is gone.
I hope this helps!
Upvotes: 1
Reputation: 238411
I know that when we have a pointer to an array, it means it is dynamically created
No. That induction is not correct. A pointer can also point to non-dynamically created arrays.
so I should delete it later in my destructor.
If your class instance is responsible for destruction of that array then destructor is indeed one place where that should probably be done.
do I need to include a delete statement in the destructor for this normal array too? for example, I have a normal array like this int myArray[];
No. Only objects created with new
are destyroyed with delete
. And only objects created with new[]
are destroyed with delete[]
. Not anything else.
or the program will do that automatically?
Yes.
Objects with automatic storage duration are destroyed automatically at the end of the scope where they are declared. That is what the name of the storage class comes from. Objects with static storage duration are destroyed after main
returns. Temporary objects are destroyed at the end of the full expression (unless their lifetime is extended by binding the object to a reference). Member variables are destroyed when the super (not meaning base class in this context) object is destroyed.
Only dynamically created objects must be destroyed manually.
While it is useful to learn how it is done, there is quite rarely need to do manual memory management in C++. The standard library provides containers that do the hard work for you. Instead of dynamically creating an array, one would typically use std::vector
.
Upvotes: 1
Reputation: 23701
But what if we have a normal array, do I need to include a delete statement in the destructor for this normal array too? or the program will do that automatically? for example, I have a normal array like this
int myArray[];
and in my destructor, should I include this:delete[] myArray;
?
No. If you don't new[]
an array, then you don't need to delete[]
it. Everything else is done automatically. This is why using std::array
, std::vector
etc. is so great: There is no need to write new[]
when you use them, so you don't have to worry that you maybe forgot a delete[]
.
"I know that when we have a pointer to an array, it means it is dynamically created"
That is not correct. You can take a pointer to basically anything, including stack variables. Dynamic allocation generally gives you back a pointer (which is probably where the confusion stems from) but not every pointer comes from dynamic allocation. Consider this:
void foo()
{
int a;
int* b = &a; // No dynamic allocation.
}
Again, since there is no new
here, there should be no delete
.
Upvotes: 2