Slimelite BBS

cchar.hpp

Back to EMULATE

From Ed Beroset · 1996-04-04 18:42 · #73606 in EMULATE

In a msg on <Apr 04 12:42>, to Ed Beroset, Chris Downs writes:

 EB>> if we explicitly force it to NULL, the next access is
 EB>> definitely not going to fetch valid data, thereby unmasking the bug.

 CD>  The problem there is that DM's implementation uses the NULL rep
 CD>  for a perfectly valid cchar.  So if you implement the class so
 CD>  that it works for valid input, setting the pointer to some other
 CD>  perfectly acceptable value shouldn't make the program crash.

That would certainly be true if it was a full-blown string class, but I believe Darin mentioned explicitly in his message that he used this thing basically as a slightly smarter array of characters specifically *because* he didn't want to use the full blown string class.  IMHO, the whole cchar class offers no significant improvement over plain char[] but that's probably because I don't share Darin's "I hate having to delete memory" sentiment.

 CD>> Why should the  value of pchar matter once the object is 
 CD>> deleted?  The only reason it  should matter is if you made a 
 CD>> boo-boo and tried to delete the thing more than once.

 EB>> ??  No, that's the only time that setting it to zero would
 EB>> mask the bug (and it isn't necessarily a bug to attempt to
 EB>> delete it more than once, IMHO).

 CD>  Well, we disagree there.  If it wasn't a "bug" to delete an 
 CD> already  deleted object  or in anyway access an already 
 CD> deleted item, then  this imposes some extra restrictions on 
 CD> all your class implementations.  

We're not talking about deleting an already deleted *object*.  I was referring to an already deleted private *member* of an object in which we may very reasonably use a NULL pointer meaning "not yet allocated."  For example, some arbitrary class definition might have a char * data member which points to dynmically allocated memory.  A constructor might set this member to NULL to indicate that no memory is allocated.  Assuming we have some external means (i.e. a member function) to cause this data member to be deleted, and also that we have some re-alloction member function, it's quite reasonable to have the internal member deleted more than once.  E.g.:

// alpha is the class as described above
alpha Spot;

// causes the internal data member to be deleted and set to NULL explicitly
Spot.clear();  
//
//  more code here...
//

// some later time, we feed Spot some new data
Spot.set("some random length data);

Spot's member function might be implemented in such a way as to delete the member data *again* knowing that because of the design, the member data is either actually pointing to valid data or is NULL (and NULL may or may not be valid data -- it makes no difference).  In either case, within the member function, before we allocate and copy in the new data, we might get rid of the old like so: 

delete[] internal;  
        
In this case, we've essentially deleted the internal data multiple times, but there is no error.

 EB>> int main()
 EB>> {
 EB>> cchar *test;

 EB>> // allocate and initialize using buggy function
 EB>> test = somefunc("nobody here but us chickens!");

 EB>> // change the first letter of the string
 EB>> (*test)[0] = 'N';

 CD>  I guess a lot depends on the debugging support of your 
 CD> environment.  The VC Win32 combo will "illegal memory access" 
 CD> abort since the test  pointer won't be pointing to valid data.  

That's why I mentioned that I had used the Borland compiler for DOS for this specific test.  In the VC/Win32 environment it made no difference, but such defensive programming under DOS, for example, makes a some sense.  I also "cheated" by altering the first byte.  If it had been changed to (*test)[100] for example, no error message would have resulted.

 CD> Only if your system traps for NULL  pointer dereferencing (is that an 
 CD> implementation requirement?) will this be true.

That's correct.  I may be misreading but I don't think it's a requirement.  Still, it's commonly implemented since it's a common error.

 CD>  But I can see your point that it could be a bug spotter if the 
 CD> only  illegal memory access your system can trap is 
 CD> dereferencing a NULL  pointer.  But again, your class 
 CD> implentation must be set up so  that a NULL internal rep is invalid.  

That doesn't follow.  It may be valid data, but not for all operations.  For example, it's certainly allowable to have an ifstream without yet having opened any file.  Using, say, the seekg() member function on an unopened ifstream will result in an error.  Similarly, NULL might be a valid internal representation for an unallocated string even though this will cause some operations to fail.  Now how we decide to *handle* these errors (or not!) is where we get into the difference between a full blown string class and something like cchar.

 CD> Heck even this seems a suspect  design decision 
 CD> since the private rep shouldn't matter  at all to the client code.  

Ideally, yes, that's true.  But there are, as you well know, some real world tradeoffs that sometimes argue against this -- I could build a bird feeder with bulletproof glass, but does it really make sense to do so?  For Darin's purposes, there were apparently some aspects of the string class that made its use undesireable for some application.  Adding all of the bells and whistles into his code, while undeniably improving its robustness and general utility, would ultimately turn it into more or less exactly the string class he had already rejected for his design, wouldn't it?

 EB>> // note that test might not be a NULL pointer
 EB>> cout << *test << endl;
 EB>> return 0;
 EB>> }

 CD>  But as I mentioned, this would be wrong anyway since the rep 
 CD> could  still legally and correctly be NULL in the first place.

Yep.  And it would still be a bug, wouldn't it?  :-)

 EB>> It may seem a contrived example, but I've seen it come up
 EB>> as a bug in the real world more often than "double
 EB>> deletion."  Is your experience different?

 CD>  Actually, that's why I mentioned it.  On one of my first C++ 
 CD> programs,  I was resetting a pointer to NULL in the 
 CD> destructor and it solved  some "crash and burn" problems.  
 CD> But it turned out that this "fix"  only masked the real 
 CD> problem which in that case was my failure to  declare the 
 CD> destructor for a base class as virtual.

I can see how that might happen.  It's fortunate that neither of us ever make such mistakes any more.  ;-)  However, I think I'd argue in this case that the problem was a bad fix, and not necessarily the practice of setting pointers to NULL on deletion of internal data members.   

-> Ed <-

--- Squish v1.01
 * Origin: = Psychotronic BBS // 919-286-4542 // Durham, NC = (1:3641/1.250)