malloc/calloc free with double pointers

L

luca

I allocate memory using a double pointer (to double values).

double** chOut = (double**) calloc ( nCol , sizeof(double*) );
for (int x = 0 ; x < nCol ; x++)
chOut[x] = (double*) calloc (nRow, sizeof(double));


What do I have to do in order to correctly free all the allocated memory?

1)
free(chOut);

Is it enough? Do I have to free each row before freeing chOut?

2)
for (int x = 0 ; x < nCol ; x++)
free(chOut[x]);
free(chOut);


Thanks
Luca.
 
R

Rolf Magnus

luca said:
I allocate memory using a double pointer (to double values).

double** chOut = (double**) calloc ( nCol , sizeof(double*) );
for (int x = 0 ; x < nCol ; x++)
chOut[x] = (double*) calloc (nRow, sizeof(double));

Note that calloc zero's out the bytes, which might not be what you want
here. Why don't you use 'new' anyway?
What do I have to do in order to correctly free all the allocated
memory?

1)
free(chOut);

Is it enough? Do I have to free each row before freeing chOut?

2)
for (int x = 0 ; x < nCol ; x++)
free(chOut[x]);
free(chOut);

2) is correct. For every malloc, you need a free call.
 
V

void

Hello!!
User luca, On 2004-03-02 12:41:
I allocate memory using a double pointer (to double values).

double** chOut = (double**) calloc ( nCol , sizeof(double*) );
for (int x = 0 ; x < nCol ; x++)
chOut[x] = (double*) calloc (nRow, sizeof(double));


What do I have to do in order to correctly free all the allocated memory?

1)
free(chOut);

Is it enough?
No.

Do I have to free each row before freeing chOut?

Yes.

Best
Darek
 
L

luca

double** chOut = (double**) calloc ( nCol , sizeof(double*) );
for (int x = 0 ; x < nCol ; x++)
chOut[x] = (double*) calloc (nRow, sizeof(double));

Note that calloc zero's out the bytes, which might not be what you want
here. Why don't you use 'new' anyway?

'new' what?
Sorry for the ignorance : I always use 'new' to create an istance of a class
but I don't know how to use it to allocate a custom size portion of the
heap.
Is it possible?
 
R

Rolf Magnus

luca said:
double** chOut = (double**) calloc ( nCol , sizeof(double*) );
for (int x = 0 ; x < nCol ; x++)
chOut[x] = (double*) calloc (nRow, sizeof(double));

Note that calloc zero's out the bytes, which might not be what you
want here. Why don't you use 'new' anyway?

'new' what?
Sorry for the ignorance : I always use 'new' to create an istance of a
class but I don't know how to use it to allocate a custom size portion
of the heap.

You allocate the memory to put something into it, I assume ;-) and so
jsut specify that type. It doesn't matter if that type is a class or a
built-in one.
Is it possible?

You could write the above as:

double** chOut = new double*[nCol];
for (int x = 0; x < nCol; ++x)
chOut[x] = new double[nRow];

and destroy it with:

for (int x = 0; x < nCol; ++x)
delete [] chOut[x];
delete [] chOut;
 
D

Default User

luca said:
double** chOut = (double**) calloc ( nCol , sizeof(double*) );
for (int x = 0 ; x < nCol ; x++)
chOut[x] = (double*) calloc (nRow, sizeof(double));

Note that calloc zero's out the bytes, which might not be what you want
here. Why don't you use 'new' anyway?

'new' what?
Sorry for the ignorance : I always use 'new' to create an istance of a class
but I don't know how to use it to allocate a custom size portion of the
heap.
Is it possible?


double** chOut = new double*[nCol];

for (int x = 0 ; x < nCol ; x++)
chOut[x] = new double[nRow];


You need to get an up-to-date C++ book and read it. Accelerated C++ by
Koeing and Moo is often recommended.

If I were you, I'd seriously investigate standard containers vs. any
dynamically-allocated array-like entities.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top