Reputation: 31
From what I'd learned, a dynamically allocated variable needs to be deleted using the delete operator and will not automatically delete at the end of a scope, like in the case of static variables.
Therefore, in the following example when the loop runs for the 2nd and 3rd time, shouldn't "int *p=new int;" be cosnidered as multiple initialization since the dynamically allocated memory 'p' hasn't been deleted?
#include<iostream>
using namespace std;
void main()
{
int i = 2;
while (i > -1)
{
int *p=new int;
*p = 5;
cout << *p;
--i;
}
}
Using Visual Studio 2015, the above program gives no error. According to my understanding this does not make sense.
I'm assuming there is something wrong with my understanding of dynamically allocated variables. Can anyone please clarify?
Upvotes: 0
Views: 952
Reputation: 396
p is just a static variable containing the address to the dynamic data, the pointer itself gets lost at the end of the scope, while the dynamic data persist without having a pointer to access it (memory leak).
The data pointed to by p is dynamic memory (in the heap) and won't get freed after each iteration, however, the content of variable p itself (which is the address of the dynamic data, if you go to p in memory you find it a simple number in stack, which is just the address of the dynamic data in the heap not the data itself) is just a static address in the stack that gets lost at the end of the scope (an iteration).
Upvotes: 0
Reputation: 66371
You can't dynamically allocate variables, only objects.
p
is not a dynamically allocated object, but *p
- the object created by new
- is.
delete p
would not delete p
but the object it points to.
Scope is a syntactic compile-time property that applies to names.
The variable p
, having a name, has a scope.
The object it points to is unnamed, so the concept of scope doesn't even apply to it.
At runtime, both p
and the object it points to have a lifetime.
The lifetime of p
coincides with its scope, since it's an automatic variable. Every iteration of the loop has its own variable, all with the same name - there is no multiple initialization because the variables are distinct.
The lifetime of the object that p
points to extends until its address is passed to delete
.
Since you never do that, every object you created with new
"leaks".
Upvotes: 0
Reputation: 2524
Your code shows what is called a "memory leak". The memory allocated for each loop iteration is lost when p
goes out of scope without delete
ing the memory first. This (usually) does not lead to compiler warnings or runtime errors, as it can be quite complicated for the compiler to find this kind of error. Some static code analysers might be able to detect this though.
What you might notice in the case of a memory leak is that your program uses up more and more memory the longer it runs, meaning that memory leaks are especially problematic in systems with low RAM and for programs that are long running, e.h. system services that are supposed to run for several days.
There are special tools to find memory leaks, e.g. valgrind for Linux or built-in tools in the debug runtime for Visual Studio.
Upvotes: 1