Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > malloc/calloc free with double pointers

Reply
Thread Tools

malloc/calloc free with double pointers

 
 
luca
Guest
Posts: n/a
 
      03-02-2004
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.


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      03-02-2004
luca wrote:

> 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.

 
Reply With Quote
 
 
 
 
void
Guest
Posts: n/a
 
      03-02-2004
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
--
I accept chaos. I am not sure whether it accepts me. I know some people
are terrified of the bomb. But then some people are terrified to be seen
carrying a modern screen magazine. Experience teaches us that silence
terrifies people the most. [Bob Dylan]
 
Reply With Quote
 
luca
Guest
Posts: n/a
 
      03-02-2004

> > 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?


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      03-02-2004
luca wrote:

>
>> > 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;

--
How come Superman could stop bullets with his chest,
but always ducked when someone threw a gun at him?

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      03-02-2004
luca wrote:
>
> > > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM



Advertisments