Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a pointer to pointer (as array of arrays)

I have this in my code:

double** desc = new double* [size_out]; for (int i = 0; i < size_out; i++)     desc[i] = new double [size_in]; 

How do I delete this desc?

Should I do:

delete [] desc; 

or

for (int i=0; i<size_out; i++)     delete [] desc[i]; delete [] desc; 

or

for (int i=0; i<size_out; i++)     delete [] desc[i]; delete desc; 

?

like image 671
yelo3 Avatar asked Nov 16 '10 12:11

yelo3


People also ask

How do you delete a pointer array?

To delete a dynamic pointer array of free store, use the operator, delete [] arrayName, in the same scope.

How do you delete a double pointer array?

2D Free Store Array as Pointer-to-Pointer Now, this pointer 2D array in free store is a pointer one dimensional array, of pointer arrays. So, in order to delete the 2d array in free store, all the rows have to be deleted first with delete[] before the principal one-dimensional pointer array is deleted.

What happens when you delete a pointer to a pointer?

The address of the pointer does not change after you perform delete on it. The space allocated to the pointer variable itself remains in place until your program releases it (which it might never do, e.g. when the pointer is in the static storage area).

What does delete [] do in C++?

The delete [] operator deallocates memory and calls destructors for an array of objects created with new [] . Using delete on a pointer returned by new [] or delete [] on a pointer returned by new results in undefined behavior.

How do you write an array in C++ with pointers?

Step 1 − Declare and read the number of elements. Step 2 − Declare and read the array size at runtime. Step 3 − Input the array elements. Step 4 − Declare a pointer variable. Step 5 − Allocate the memory dynamically at runtime.

How to delete an element from an array in C++?

Step 1 − Declare and read the number of elements. Step 2 − Declare and read the array size at runtime. Step 3 − Input the array elements. Step 4 − Declare a pointer variable. Step 5 − Allocate the memory dynamically at runtime. Step 6 − Enter an element that to be deleted. Step 7 − After deletion, the elements are shifted to left by one position.

What is a pointer to an array called?

Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array. int a[3] = {3, 4, 5 }; int *ptr = a; We have a pointer ptr that focuses to the 0th component of the array.

How do you access a 2 dimensional array with a pointer?

Pointer to Multidimensional Arrays Pointers and two dimensional Arrays: In a two dimensional array, we can access each element by using two subscripts, where first subscript represents the row number and second subscript represents the column number. The elements of 2-D array can be accessed with the help of pointer notation also.


2 Answers

Simple rules to follow:

  • for each allocation, there has to be a deallocation (ex1 is therefore wrong)
  • what was allocated using new should be freed using delete, using new[] should be deallocated using delete[] and using malloc should be deallocated using free (ex3 is therefore wrong)

Conclusion, ex2 is OK.

like image 102
Šimon Tóth Avatar answered Oct 09 '22 07:10

Šimon Tóth


Your code shouldn't compile. The type of an array new expression is a pointer to the type of array element being created (the value is a pointer to the first element of the allocated array).

So the type of new double**[size_out] is double ***.

Whenever you use the array form of new, you must use the array form of delete even if you only allocate an array of size one.

double*** desc = new double**[size_out]; for (int i=0; i<size_out; i++)     desc[i] = new double*[size_in];   for (int i=0; i<size_out; i++)     delete[] desc[i];  delete[] desc; 

Note that you still haven't allocated any double, just pointers.

Did you really want this instead?

double** desc = new double*[size_out]; for (int i=0; i<size_out; i++)     desc[i] = new double[size_in];  for (int i=0; i<size_out; i++)     delete[] desc[i];  delete[] desc; 
like image 21
CB Bailey Avatar answered Oct 09 '22 09:10

CB Bailey