Home > Net >  How to delete memory for object created by *new
How to delete memory for object created by *new

Time:11-04

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

CodePudding user response:

is there any memory leakage

Yes. You created an int via new and immediately loose any reference to the pointer.

If so how to clear memory

You cant. num is just holds a copy of the value. You have no way to delete the actual int that was dynamically allocated.

Also defining int by * new is the only option to use and not int num = 25;

That makes no sense. Maybe there are things you didn't mention (odd requirements, weird assignment, or similar), but based on what you say, this is the correct way of "managing the memory":

for(int i = 0;i < 200;i  )
{
   int num = 25;
}

num has automatic storage duration. Its memory will be freed when it goes out of scope.

I tried many ways but delete and free does not work

For delete you need the pointer returned by new. You don't have that. Mixing free with new is wrong.

CodePudding user response:

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.

What's wrong with your code snippet?

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;
}

TL;DR

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

  • Related