malloc/calloc free with double pointers

Discussion in 'C++' started by luca, Mar 2, 2004.

  1. luca

    luca Guest

    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.
     
    luca, Mar 2, 2004
    #1
    1. Advertising

  2. luca

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Mar 2, 2004
    #2
    1. Advertising

  3. luca

    void Guest

    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]
     
    void, Mar 2, 2004
    #3
  4. luca

    luca Guest


    > > 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?
     
    luca, Mar 2, 2004
    #4
  5. luca

    Rolf Magnus Guest

    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?
     
    Rolf Magnus, Mar 2, 2004
    #5
  6. luca

    Default User Guest

    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
     
    Default User, Mar 2, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jun Woong

    Re: Override malloc,calloc,realloc and free?

    Jun Woong, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,113
    Jun Woong
    Jun 26, 2003
  2. Dan Pop
    Replies:
    0
    Views:
    919
    Dan Pop
    Jun 26, 2003
  3. Douglas A. Gwyn

    Re: Override malloc,calloc,realloc and free?

    Douglas A. Gwyn, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    761
    Douglas A. Gwyn
    Jun 26, 2003
  4. Dan Pop
    Replies:
    2
    Views:
    2,086
    Jun Woong
    Jun 27, 2003
  5. Myth__Buster
    Replies:
    23
    Views:
    1,163
    Nobody
    Jun 26, 2012
Loading...

Share This Page