0

I am pointing to the address of a 2D array, and I am confused as to how to dereference the pointer to free up the memory again. (I don't use "->" or "*". Is that wrong?)

My code:

double array[12][12];
//filled with numbers

double *arrayPtr; //pointer

arrayPtr = &array[0][0]; //pointing to address

multiply(arrayPtr, arrayPtr); //this works fine

//Do I need to do anything further to make sure my memory management is correct?  And if so, why?
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
MrHappyAsthma
  • 6,332
  • 9
  • 48
  • 78

3 Answers3

4

In this case, the answer is no -- since you simply defined array (didn't use something like malloc to allocate it) you don't have to do anything to free it either. If it was local (defined inside a function) it'll be freed automatically when you exit the function. If you defined it outside any function, it's a global, so it'll exist the entire time the program runs. Either way, you don't have to d any explicit memory management.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thank you very much for that. Easy to understand and answered my question. :D *I have to wait another few minutes to accept, but i will to ensure others can also see this solution – MrHappyAsthma May 23 '12 at 23:10
2
double array[12][12];

You're declaring array on the stack. It's not dynamically allocated with the heap, so you don't "free up the memory".

double *arrayPtr; //pointer
arrayPtr = &array[0][0]; //pointing to address

If you want to point to the first element, this would suffice:

double* arrayPtr = array;
Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
0

First, there a quite different between C and C++

In C to use memory management you shall use malloc/calloc/realloc and free. In c++ you will use new and delete.

In your code.

double array[12][12];

This is imply to allocate memory in stack. So the memory will be allocated to the scope of this program section so that it will be green when the scope of this variable end.

If you will to use free you will need

 double **array;
 array = (double **) malloc(sizeof(double*));
 *array = (double*) malloc (24 * sizeof(double));

free (*array);
free (array);
Agus
  • 1,604
  • 2
  • 23
  • 48
  • There's quite a lot wrong with this code... namely the casts are not only wrong but unnecessary and in this case illegal (I'm quite you cannot cast a pointer type to a floating type). Also, the syntax is wrong for the `free` part... the `()` are missing. – dreamlax May 23 '12 at 23:44
  • sorry, i not understand your comment, when you work pointer, and in C that;s is less restricted that c++, you are allow to use () casting. Additionally, the casting imply interpret this memory section as the type cast you are doing. http://www.cplusplus.com/doc/tutorial/typecasting/ – Agus May 23 '12 at 23:51
  • I know what casting is, the point is that you don't *need* to cast the result of `malloc`. In C, `void *` can be implicitly converted to any other pointer type without a cast. See [this question](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) about why you should **not** cast the result of `malloc`. – dreamlax May 23 '12 at 23:56