Uninitialized memory blocks in VC ++

As you know, Visual C ++ denotes uninitialized or simply freed memory blocks with special non-zero markers. Is there a way to completely disable this behavior without setting all uninitialized memory to zeros? This causes chaos with my valid non-null checks, since 0xFEEEFEEE != 0.

Hmm, maybe I should explain a little better. I create and initialize a variable (via a new one) and everything goes fine. When I free it (via delete), it sets a pointer to 0xFEEEFEEEinstead NULL. When I insert the proper check for NULL, since all good programs that manage their own memory should have problems, as it 0xFEEEFEEEpasses the test NULLwithout problems. Is there any good way, besides manually setting all the pointers to NULLwhen they are deleted, to determine when the memory is already freed? I would prefer not to use Boost simply because I do not need overhead, although this may be small, as this is the only one I would use Boost for.

+5
source share
15 answers

From deleteto reset, not all pointers to an object are on NULL. Also, you should not change the default fillable memory for DEBUG windows, and you should use some kind of thing, as boost::shared_ptr<>for pointers, anyway.

However, if you really want to shoot yourself in the foot , you can.

You can change the default padding for Windows DEBUG runtime using such a highlighting mechanism. This will only work with the selected HEAP object!

int main(int argc,char** arv)
{
  // Call first to register hook    
  _CrtSetAllocHook(&zero_fill);
  // Do other stuff
  malloc(100);
}


int zero_fill(int nAllocType, 
              void* pvData, 
              size_t nSize,
              int nBlockUse, 
              long lRequest, 
              const unsigned char *szFileName, 
              int nLine )
{
  /// Very Importaint !! 
  /// infinite recursion if this is removed !!
  /// _CRT_BLOCK must not do any thing but return TRUE
  /// even calling printf in the _CRT_BLOCK will cause
  /// infinite recursion
  if ( nBlockUse == _CRT_BLOCK )
    return( TRUE );
  switch(nAllocType)
  {
  case _HOOK_ALLOC:
  case _HOOK_REALLOC:
    // zero initialize the allocated space.
    memset(pvData,0,nSize);
    break;
  case _HOOK_FREE:
    break;
  }
  return TRUE;
}
+6
source

, explicity NULL. delete. ( ) .

, (, boost::shared_ptr), , .

+18

V++ , . 0xfeeefeee, ( ), .

+14

, , , "". . , - . - C/++.

Java ( #, ) , , , , . C, .

+8

Release Debug, , . not - memset(), ZeroMemory() SecureZeroMemory(), -, , . undefined.

+6

:

( new), . ( delete), 0xFEEEFEEE NULL. NULL, , , , , 0xFEEEFEEE NULL- .

MSVC , - , , ( NULL). , , , , .

, , , . , , .

+5

V++ ( , ), . , . ++, NULL, - . , NULL.

+4

@Jeff Hubbard ():

, : pvData NULL _HOOK_FREE 0xFEEEFEEE .

, , NULL (.. ).

.

"", , , . - - , .

+4

, - .

, . , , NULL, , , - "". - , - , "" "". . , "".

, , .

char * p = new char[16];     // 16 bytes of random trash
strcpy(p, "StackOverflow");  // 13 characters, a '\0' terminator, and two bytes of trash
delete [] p;                 // return 16 bytes to the heap, but nothing else changes;

if (p != NULL)               // Why would p be NULL?  It was never set to NULL
    ASSERT(p[0] == 'S');     // In debug, this will crash, because p = 0xfeeefeee and 
                             // dereferencing it will cause an error.
                             // Release mode may or may or may not work, depending on
                             // other memory operations

, NULL delete. , boost - .

+4

, , .

. .

0xFEEEFEEE , delete .

. , , 0xfeeefeee, 0xfeeefeee,..., 0xfeeefeee.

, ( 0xfeeefeee DEBUG), .

+3

@[ ]:

, , . , 0xFEEEFEEE , delete . , , .

- , , , , _CrtSetAllocHook().

0xFEEEFEEE (. http://www.nobugs.org/developer/win32/debug_crt_heap.html). - , ?

+1

, , , , .

, 0, , 2 .

int *ptr=0;

NULL, 0 ( , , , windows.h, !

0

malloc, . . 0, "calloc", malloc ( , 1, malloc). calloc, , .

http://wiki.answers.com/Q/What_is_the_difference_between_malloc_and_calloc_functions

0

Why not create your own #define and get used to using it?

those.

#define SafeDelete(mem) { delete mem; mem = NULL; }
#define SafeDeleteArray(mem) { delete [] mem; mem = NULL; }

Obviously, you can name it whatever you want. deleteZ, deleteesafe, no matter what you are comfortable with.

0
source

You can also create a memory manager. You can then redefine the new one and delete it to pull out / return the previously allocated memory cartridge.

0
source

All Articles