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;
?
To delete a dynamic pointer array of free store, use the operator, delete [] arrayName, in the same scope.
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.
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).
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.
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 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.
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.
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.
Simple rules to follow:
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With