how to use "delete"? 
Author Message
 how to use "delete"?

(this might be a simple question for you, but for me I have to ask for your
great help,thanks)

Say, there is member var of my class,
        char *myStr;

it is initialized with
       myStr = new char[100];

and it could be used at many places inside my program. I want delete it
inside the destructor of my class.

but before deleting it, how to know if it can be deleted or not?
       --------------------------------------------------
       /*check myStr, if its memory had been freed or */
       /*it was not initialized sucessfully at all, then       */
       /*do not need to delete it                                  */
       /*Q: HOW TO KNOW ?                                */

       /*if its memory has not been freed yet, then delete*/
       delete [] myStr;
      ----------------------------------------------------

Thanks a lot



Fri, 18 Jul 2003 17:43:17 GMT  
 how to use "delete"?

Quote:
>Say, there is member var of my class,
>        char *myStr;

>it is initialized with
>       myStr = new char[100];

>and it could be used at many places inside my program. I want delete it
>inside the destructor of my class.

char *szString= NULL;

        // myshterioush shings happensh

if (szString)
{
  delete [] szString;
  szString= NULL;

Quote:
}

The nulling of a deleted pointer ought to be mandatory.

btw: I don't think this is a *de{*filter*}* question, try one of the
*vc*-groups and don't forget to check the FAQ befor you post.

HTH

Carsten Witte [MVP]

--
  error S4711: .signature not found



Fri, 18 Jul 2003 18:06:10 GMT  
 how to use "delete"?
Deleting a NULL pointer is safe, so you do not need to check:
if( szString ).



Quote:

> >Say, there is member var of my class,
> >        char *myStr;

> >it is initialized with
> >       myStr = new char[100];

> >and it could be used at many places inside my program. I want delete it
> >inside the destructor of my class.

> char *szString= NULL;

> // myshterioush shings happensh

> if (szString)
> {
>   delete [] szString;
>   szString= NULL;
> }

> The nulling of a deleted pointer ought to be mandatory.

> btw: I don't think this is a *de{*filter*}* question, try one of the
> *vc*-groups and don't forget to check the FAQ befor you post.

> HTH

> Carsten Witte [MVP]

> --
>   error S4711: .signature not found



Fri, 18 Jul 2003 21:35:38 GMT  
 how to use "delete"?

Quote:
>Deleting a NULL pointer is safe, so you do not need to check:
>if( szString ).

Of course you're perfectly right... *but* I--stubborn as I am--intend on
keeping with the tradition of checking it anyway. Shmaybe some old
fashioned compatebility issue on my behalf. ;)

Carsten Witte [MVP]

--
  error S4711: .signature not found



Fri, 18 Jul 2003 22:23:29 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. MFC Container Classes using different "new" and "delete"

2. Why can only undo "Delete"?

3. On using "using"

4. Delete "/" from char pointer

5. Deleting with "fwrite()"

6. ODBC, VFP and "real" deletes

7. "delete []" in MSDN

8. Question about the operator "delete"

9. "scalar deleting destructor"

10. C++ question with "delete"

11. Error when "delete[] m_pHashTable" in CMap

12. SUMMARY: "Deleting every other byte"

 

 
Powered by phpBB® Forum Software