Re: [Plib-devel] strdup...no!
Brought to you by:
sjbaker
From: Steve B. <sjb...@ai...> - 2000-05-16 21:07:17
|
> Dave McClurg wrote: > > > I just downloaded the CVS of PLIB - and I see 'strdup' calls > > everywhere... > > one of which is causing a core dump. > > > > Please don't use strdup under C++ because it circumvents C++'s memory > > allocator. > > > OT: > > I thought memory allocation in C++ was backwards compatible with C except in regards to casts from void * to/from other pointer types. > > Could you explain this or give me an URL to read? Well, the idea is that if you use 'new' and 'delete' for everything, then someone in the future could overload those operators to use some other memory allocator. I've done this in the past in order to use shared memory - or fancy hardware gizmo's called 'ScramNet' cards...also to check for memory leaks and corruptions. It also allows you to check for running out of memory in one nice central location. If you were rigerous about it and always used 'free' to remove everything that had been 'malloc'ed or 'strdup'ed then it would be OK-ish (still not great - but OK). However, if you strdup something and then remove it using 'delete', then great and subtle mahem will happen downstream. So, it's really NOT a good idea to mix C and C++ style memory allocators. -- Steve Baker http://web2.airmail.net/sjbaker1 sjb...@ai... (home) http://www.woodsoup.org/~sbaker sj...@ht... (work) |