Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,711 of 33,346    |
|    Francis Glassborow to A. McKenney    |
|    Re: Cost of deleting a null pointer    |
|    01 Dec 11 17:09:00    |
   
   bab5a4e7   
   From: francis.glassborow@btinternet.com   
      
   On 01/12/2011 03:29, A. McKenney wrote:   
   > Recently, someone was doing some profiling of our code, and found that   
   > code essentially like the following was taking far more time than   
   > expected:   
   >   
   >   
   > class T { /* .... */ };   
   >   
   > T tlist[18];   
   so tlist is a list of T   
   > ...   
   >   
   > void reset()   
   > {   
   > for ( int i = 0; i< 18; ++i )   
   > {   
   > delete tlist[i];   
   but delete takes a pointer so here is your problem.   
      
   > tlist[i] = 0;   
   compounded here where you are attempting to set a T to zero.   
   > }   
   > }   
   >   
   > In most cases, all the elements of tlist were null.   
      
   Really, not in the code above. Assuming that it is possible to assign 0   
   to a T, most of the elements of T are the result of this assignment.   
   Without seeing the definition of T I cannot say more.   
      
   >   
   > He found that if he replaced   
   >   
   > delete tlist[i];   
   >   
   > with   
   >   
   > if ( tlist[i] ) delete tlist[i];   
   If this is valid code there must be something in T that is a little   
   unexpected.   
      
   >   
   > it sped the function up by about a factor of 30. His assumption was   
   > that all the destructor and freeing logic was being gone through   
   > even when the pointer was null.   
   >   
      
   I suspect the code he is using is somewhat different to the code you   
   show above but is still flawed somewhere.   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca