|
From: Per W. <pw...@ia...> - 2003-07-14 23:45:13
|
In your text you mentioned something about the original code zeroing arrays on erase and performing reallocation. Just a note about a very important difference between C and C++. In C it is always safe to use memset() or ZeroMemory etc to clear a struct directly after allocation. You might also do it at any other time during the life of the struct with one little caveat. If the struct contained pointers to other data you might introduce a memory leak. However, in C++ it is not generally safe to perform memset() of a struct. If the object in question either is part of a multiple-inherritance hierarchy, has virtual methods, supports dynamic typecast etc, memset() might erase hidden data fields that are initialized by the object constructor. This kind of problem is very easily introduced in source code that originated as C, and then gradually transformed into C++. The result of overwriting a VMT - Virtual Method Table - field of an object is fatal and is guaranteed to bite hard. One other thing is that the STL code assumes that it controls the memory, and does not like someone else to try and reallocate items. The actual containers may not be reallocated, and if individual elements of the containsers needs to be reallocated, they should first be removed from the container, then reallocated and inserted again. /Per W On Mon, 14 Jul 2003, Shane Bryldt wrote: > The original implementation used some very obscure direct > manipulation of array's, such that it would zero out array's when they > weren't used, yet left them allocated. And in some other cases, would > actually call realloc to reallocate the memory. |