FidoNet Echomail Archive
aust_c_here

<<< Previous Index Next >>>

From: David Nugent
To: Frank Adam
Date: 1997-02-14 03:31:00
Subject: Strdup fun

 > A bit of C++ won't hurt anyone ?
 > I've had a bit of fun with placing an explicit free inside a destructor
 > it took out the CMOS twice out of three crashes. Bit harsh :-)

Well, you will insist on running an antique operating system...

 > Anyway i was allocating memory through strdup() and i wouldn't have
 > thought the class had a clue about that. How come it does ?
 > My theory is that i freed the buffer then the destructor tried to do the
 > same, but i didn't think that freeing a variable twice could have this
 > effect.

I don't think this is the precise problem. I wouldn't be surprised if the
problem was actually elsewhere - probably data used by malloc() is getting
trashed.

The "correct" way to dispose of a buffer allocated via malloc(),
or indirectly so via strdup() is to use free(). It is a bad idea to mix and
match new/delete new[]/delete[] with free() and malloc() since the outcome
is undefined.


 > Btw, anyone knows if strdup() would call realloc() if passed an already
 > allocated buffer ?

Not in any implementation of strdup() I've ever seen. The whole idea is to
duplicate the string, not reallocate it.

--- MaltEd/2 1.0.b6
 * Origin: Unique Computing Pty Limited (3:632/348)
SEEN-BY: 50/99 54/99 620/243 623/630 632/103 107 348 360 601 633/374 635/544
SEEN-BY: 635/728 639/252 640/820 711/413 430 934 712/311 407 505 506 517 623
SEEN-BY: 712/624 704 841 713/317 714/906 800/1
@PATH: 632/348 360 50/99 712/624 711/934


<<< Previous Index Next >>>