Is it possible to delete a dynamic allocated through another pointer?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int * fn1()
{
     int * v = new int;
     return v;
}

void main()
{
     int * ptr = fn1();
     delete ptr;/////this kind of deletion doesn't cause memory leak right?

     int *arryptr = new int[2];

/////////Is this 2 lines below equivalent to delete[] arryptr? ////// 
     delete &(arryptr[0]);
     delete &(arryptr[1]);
}
Last edited on
For the first bit, it all looks correct (apart from the 'void main' bit). As for line 15 and 16, I may be wrong, but I don't think so. From memory, it isn't defined in the standard whether that should work, and therefore different compilers might have special things that they do with ::operator new[] that requires ::operator delete[].
closed account (2AoiNwbp)
As far as I am concerned, it is responsability of the person who asks for memory allocation, to deallocate it.
Thus, if someone else uses the function fn1(), he or she doesn't even should/could know that the implementer of that function used operator new.
A more clear code would be:
1
2
3
4
5
void fn1(int& v) {
  ...
  // do some stuff
  return;
}

Regarding your specific question:
Is it possible to delete a dynamic allocated through another pointer?
The answer is yes, and that is the case of a move constructor. (i.e.):
1
2
3
4
5
6
    Example6& operator= (Example6&& x) {
      delete ptr; 
      ptr = x.ptr;
      x.ptr=nullptr;
      return *this;
    }

Here, the memory allocated to by x for ptr will be deleted by the destructor of *this.

regards,
Alejandro
Last edited on
First, maybe in Windows and other OSes it's different, but I've implemented these operators for a kernel. malloc and free creates a header and footer around the allocated memory, and from these it knows how much should it use, how much to free, and it's also good for error checking.

So, new is different, because it calls the constructors too. For a single (non-array) new, it creates the memory(with footer/header) and "copies" the constructed member to that location. For free/delete, it firstly checks the header/footer, delete calls destructors then deallocates the memory, e.g with free (mark it as free in a bitmap, and remove the header/footer).

If you call a single delete on an array, it tries to deallocate that only element, but because it only sees the header/or nothing, because the header is only at the beginning of the memory, it will fail, or will not destruct the objects, just deallocate the memory. This can lead to memory leaks.

E.G:

1
2
CLASS* obj = new CLASS;
CLASS* obj2 = new CLASS;


memory layout:

header_memoryobj_footer header_memoryobj2_footer

CLASS* obj = new CLASS[2];

header_memoryobj[0]_memoryobj[1]_footer
Last edited on
If you call a single delete on an array, it tries to deallocate that only element


If I loop the deletion on every single element in an array like :

1
2
3
4
5
6
7
8

int *ary = new int [10];

for (int i = 0; i < 10; i++)
{
     delete &(ary[i]);
}

well normally i think the delete [] can do better work... but i just wonder if it is possible to delete the array completely in this way
If you allocate an object with new you have to use delete to deallocate it.

If you allocate an array with new[] you have to use delete[] to deallocate it.

So no, you can't allocate an array and then deallocate only certain elements. You have to deallocate the whole array, or not deallocate at all.
About:
1
2
3
4
5
6
int *ary = new int [10];

for (int i = 0; i < 10; i++)
{
     delete &(ary[i]);
}


I think you misunderstood dynamically allocated C arrays, this could be achieved however by having each element another int pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int MAX_ARRAY = 10;
int** ary = new int* [MAX_ARRAY];

for (int i = 0; i < MAX_ARRAY; i++)
{
  ary[i] = new int(0); //Default value
}

for (int i = 0; i < MAX_ARRAY; i++)
{
  std::cout << *ary[i] << std::endl;

  delete ary[i];
  ary[i] = NULL;
}


But what I think you want to do is use a vector array or a list? You could then erase elements without using pointers to do the dirty work.
Krisando, do u still need to "delete [] ary;" do delete the memory allocated for value pointed by ary?
Yes he does. For every new[] there should be a delete[].
Ok thanks guys, i guess i get what should i know about the delete and delete[]
Topic archived. No new replies allowed.