Question about delete/new

Discussion in 'C++' started by pedicini, Oct 13, 2004.

  1. pedicini

    pedicini Guest

    I work with many dynamically allocated variables in
    my program including double **, int *, char *.

    For ex:

    double **d;
    d = new (double *) [10];
    for(int i = 0; i < 10; i++) {
    d = new double[10];
    }

    to setup a 2d array of doubles.

    My memory clear function looks like this:

    clearDoubleArray(double **d, int rows)
    {
    for(int i = 0; i < rows; i++) {
    delete [] d;
    }

    delete [] d;

    d = NULL;
    }

    In my program, the same variable is allocated and deallocated a great deal
    of times.
    Sometimes I'm not sure whether d has been initialized, or has been deleted.
    Is it safe
    to call clearDoubleArray() on a var that has not been allocated? Or should I
    keep track?
    Just for the sake of argument, assume that whenever d has not been
    allocated, that rows = 0,
    so it does not use the delete[] d loop shown above.

    Will delete[] d cause problems?

    Also, I try to set d to NULL on the last statement of the function, but it
    does not change
    where d is pointing to. How can I fix that?

    Thanks
    Shaun
     
    pedicini, Oct 13, 2004
    #1
    1. Advertising

  2. "pedicini" <> wrote...
    >I work with many dynamically allocated variables in
    > my program including double **, int *, char *.
    >
    > For ex:
    >
    > double **d;
    > d = new (double *) [10];
    > for(int i = 0; i < 10; i++) {
    > d = new double[10];
    > }
    >
    > to setup a 2d array of doubles.
    >
    > My memory clear function looks like this:
    >
    > clearDoubleArray(double **d, int rows)


    No return type?

    > {
    > for(int i = 0; i < rows; i++) {
    > delete [] d;
    > }
    >
    > delete [] d;
    >
    > d = NULL;
    > }
    >
    > In my program, the same variable is allocated and deallocated a great deal
    > of times.
    > Sometimes I'm not sure whether d has been initialized, or has been
    > deleted.
    > Is it safe
    > to call clearDoubleArray() on a var that has not been allocated? Or should
    > I
    > keep track?


    (a) You should keep track at least to know what 'rows' to pass in.
    (b) If 'd' is NULL, delete or delete[] will be OK, but an attempt to
    dereference it to get d is going to fail.
    (c) Every time you deallocate I recommend setting it to NULL.

    > Just for the sake of argument, assume that whenever d has not been
    > allocated, that rows = 0,
    > so it does not use the delete[] d loop shown above.
    >
    > Will delete[] d cause problems?


    Not if 'd' is NULL. If it's _uninitialised_, it contains garbage.
    An attempt to 'delete[]' will cause undefined behaviour.

    >
    > Also, I try to set d to NULL on the last statement of the function, but it
    > does not change
    > where d is pointing to. How can I fix that?


    Pass 'd' by reference:

    void clearDoubleArray(double ** &d, int rows)
    ...

    Victor
     
    Victor Bazarov, Oct 13, 2004
    #2
    1. Advertising

  3. pedicini

    Chris Guest

    "pedicini" <> wrote in message
    news:ckhq7a$47c$...
    >I work with many dynamically allocated variables in
    > my program including double **, int *, char *.


    try let the STL do the work for you:

    typedef std::vector< double > tDoubleVector;
    typedef std::vector< tDoubleVector > t2D_DoubleVector;

    typedef std::auto_ptr< int > tIntPtr;

    etc.........


    -c
     
    Chris, Oct 13, 2004
    #3
  4. pedicini

    pedicini Guest

    "Victor Bazarov" <> wrote in message
    news:0k0bd.117331$He1.23104@attbi_s01...
    > "pedicini" <> wrote...
    > >I work with many dynamically allocated variables in
    > > my program including double **, int *, char *.
    > >
    > > For ex:
    > >
    > > double **d;
    > > d = new (double *) [10];
    > > for(int i = 0; i < 10; i++) {
    > > d = new double[10];
    > > }
    > >
    > > to setup a 2d array of doubles.
    > >
    > > My memory clear function looks like this:
    > >
    > > clearDoubleArray(double **d, int rows)

    >
    > No return type?


    forgot, void clearDoubleArray

    >
    > > {
    > > for(int i = 0; i < rows; i++) {
    > > delete [] d;
    > > }
    > >
    > > delete [] d;
    > >
    > > d = NULL;
    > > }
    > >
    > > In my program, the same variable is allocated and deallocated a great

    deal
    > > of times.
    > > Sometimes I'm not sure whether d has been initialized, or has been
    > > deleted.
    > > Is it safe
    > > to call clearDoubleArray() on a var that has not been allocated? Or

    should
    > > I
    > > keep track?

    >
    > (a) You should keep track at least to know what 'rows' to pass in.
    > (b) If 'd' is NULL, delete or delete[] will be OK, but an attempt to
    > dereference it to get d is going to fail.
    > (c) Every time you deallocate I recommend setting it to NULL.
    >
    > > Just for the sake of argument, assume that whenever d has not been
    > > allocated, that rows = 0,
    > > so it does not use the delete[] d loop shown above.
    > >
    > > Will delete[] d cause problems?

    >
    > Not if 'd' is NULL. If it's _uninitialised_, it contains garbage.
    > An attempt to 'delete[]' will cause undefined behaviour.


    so if I had:

    double **d;
    delete [] d;

    This would cause undefined behavior? is that right?
    But this is okay:
    double **d = NULL;
    delete [] d;

    Thanks for your help Victor,
    Shaun


    >
    > >
    > > Also, I try to set d to NULL on the last statement of the function, but

    it
    > > does not change
    > > where d is pointing to. How can I fix that?

    >
    > Pass 'd' by reference:
    >
    > void clearDoubleArray(double ** &d, int rows)
    > ...
    >
    > Victor
    >
    >
     
    pedicini, Oct 13, 2004
    #4
  5. pedicini wrote:
    > [...]
    > so if I had:
    >
    > double **d;
    > delete [] d;
    >
    > This would cause undefined behavior? is that right?


    Yes.

    > But this is okay:
    > double **d = NULL;
    > delete [] d;


    Yes.

    V
     
    Victor Bazarov, Oct 13, 2004
    #5
    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. HeroOfSpielburg
    Replies:
    1
    Views:
    395
    Alf P. Steinbach
    Aug 6, 2003
  2. Jef Driesen
    Replies:
    1
    Views:
    506
    Gernot Frisch
    Jan 19, 2005
  3. DamonChong
    Replies:
    11
    Views:
    575
    msalters
    Jan 31, 2005
  4. X X
    Replies:
    4
    Views:
    358
    red floyd
    Jul 19, 2010
  5. Replies:
    2
    Views:
    470
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page