I have question regarding memory management in c++ In below example is there any memory leakage. If so how to clear memory. Also defining int by * new is the only option to use and not int num = 25;
int main()
{
for(int i = 0;i < 200;i++)
{
int num = * new int(25);
}
I tried many ways but delete and free does not work
OP: Also defining int by * new is the only option to use and not int num = 25;
No, in C++
you have different ways of create new data. In short, you can create new data automatically, dynamically or statically.
automatic storage duration
A a obj;
Here you're creating an object with automatic storage duration, this means, the object will be cleaned up when it goes out of scope. This object is created directly in the current active stack frame
, so is directly accesible via identifier.
dynamic storage duration
Dynamic storaged objects are created with the new
operator. This objects are stored on the heap
, which is an unordered (mostly) region of memory reserved to handle heap allocations
. The heap
isn't able to talk with the stack regions directly, but we can communicate the stack
and the heap
program's data with pointers.
The new
operator called in front on an object constructor, will perform a heap
allocation, placing the value in some place of the heap
and returning a pointer
to the memory address where the value resides, available in the active stack.
A* a obj = new A();
Here, we are adquiring a resource that, as C++
developers, we must take care. So, when we end with our a
object, we must tell the compiler that we finished with it and that piece of memory should be released or freed. We acomplish this in C++
with the delete
keyword.
A* a obj = new A();
/* snip */
delete a;
If we don't delete the dynamically constructed resource, we will produce a memory leak. This is, we lose the access (the returned pointer) to the resource, and we can't no longer reach it. So, that resource w'd be using memory until the OS
claim by itself that memory (and that's could also not happen), being in a undefined behaviour state, potentially leading to crashes and other things.
static storage duration
The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with static or extern
thread storage duration
The storage for the object is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the object. Only objects declared thread_local have this storage duration. thread_local can appear together with static or extern to adjust linkage.
int num = * new int(25);
You are dynamically initializing a variable with a value. This is because you are inmediatly dereferencing
the pointer that the new
operator returns, losing your access to the heap
allocated variable. delete
won't work, because you need a pointer type to apply the operator over it, and delete the resource beyond the pointer, that will be the result of the expression new int (25)
. This will return an int*
that will store the address of the dynamically initialized integer.
Probably, you're looking for something like:
for(int i = 0;i < 200;i++)
{
int* num = new int(25);
/* snip */
delete num;
}
Your deferencing, or following the pointer to the memory address that the new
keyword applied on the int
constructor returns to you, and then saving the value (not the pointer!) in the int num
variable, losing the access to the heap allocated data, and causing a memory leak