Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

free a double pointer

Tags:

c

pointers

I created a 2-D matrix using double pointer like that:

int** pt; pt = (int*) malloc(sizeof(int)*10);

I know that a pointer is freed like that

free(ptr);

How can we free the double pointer?

What if we print something and later freed that memory and exit the program? Does the final memory consist of that which we used or it will be same as initial?

like image 644
bledi Avatar asked Jun 13 '12 12:06

bledi


People also ask

Can we free a pointer?

delete and free() in C++ In C++, the delete operator should only be used either for the pointers pointing to the memory allocated using new operator or for a NULL pointer, and free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer. It is an operator.

What happens when you free a pointer?

The function free takes a pointer as parameter and deallocates the memory region pointed to by that pointer. The memory region passed to free must be previously allocated with calloc , malloc or realloc . If the pointer is NULL , no action is taken.

What does double pointer mean?

So, when we define a pointer to a pointer. The first pointer is used to store the address of the variable. And the second pointer is used to store the address of the first pointer. That is why they are also known as double-pointers.

How do I delete a 2D pointer?

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.


2 Answers

Say you have a matrix mat

int** mat = malloc(10 * sizeof(int*));
for (int i=0; i<10; ++i) {
  mat[i] = malloc(10 * sizeof(int));
}

then you can free each row of the matrix (assuming you have initialized each correctly beforehand):

for (int i=0; i<10; ++i) {
  free(mat[i]);
}

then free the top-level pointer:

free(mat);

For your second question: if you allocate memory and use it, you will change that memory, which will not be "reverted" even if you free it (although you will not be able to access it reliably/portably any more).

Note: the top-level malloc is using sizeof(int*) as you are allocating pointer-to-ints, not ints -- the size of int* and int are not guaranteed to be the same.

like image 187
Attila Avatar answered Nov 03 '22 09:11

Attila


If your matrix isn't "ragged", i.e. all rows have the same length, you might want to consider:

  1. Accessing it manually, i.e. just treat it as a 1D array of values, and keep a separate width value. To access an element at (x,y) use mat[y * width + x].
  2. If you really want the convenience of mat[y][x], you can improve it by doing a single call to malloc() that allocates both the pointer array and all the rows, then initializing the pointers to point at each row. This has the advantage that it can all be free:ed with a single free(mat); call.

The second approach would look something like this:

double ** matrix_new(size_t width, size_t height)
{
  double **p = malloc(height * sizeof *p + width * height * sizeof **p);
  double *e1 = (double *) (p + height);
  size_t i;

  for(i = 0; i < height; ++i)
    p[i] = e1 + i * width;
  return p;
}

Note: the above is un-tested, and production code should obviously check for failure before using p.

like image 2
unwind Avatar answered Nov 03 '22 11:11

unwind