This me being dumb?

Started by
4 comments, last by persil 18 years, 11 months ago
I can't seem to get this work. The background is that i'm experimenting with overloading new and delete and have my own versions. I want to track every byte that is allocated and deallocated. So i have written some code in a CPP file with a header, not a class, and i'm getting an exception.

#include "k_alloctable.h"


std::map< void*, size_t > entries;

void Log( void* p, size_t s )
{
	std::map< void*, size_t >::iterator i;

	if ( p == NULL )
		return;

	i = entries.find( p );  // *

	if ( entries.end() == i )
		return;

	//entries[ p ] = s;

}


I get an exception on the line marked with an asterisk. The problem is that the entries map is not initialised properly. I'm confused, i could have been sure that is woulf have been initialised like this. Oh and before anyone suggests it i can't put on the heap because the new operator brings me here again. Any ideas. ace
Advertisement
entries is not a pointer, so you should be using entries.find and entries.end

but this shouldn't compile at all, so I don't see why you're getting exceptions.
I may sound stupid, but... Isn't Map using new all over the place to create its nodes? So, effectively, it may be running an infinite loop and overloading the stack?

EDIT

rick_appleton: damn right, why didn't I notice it :)
Yeah sorry i forgot to change the -> to . It may be the case that map stores by value on the heap.

In which case i could simply have an array of structs and realloc it to resize it.

Does this make sense?

ace
Some part of the C++ runtime library is using operator new and running before initialization of the global variable entries is actually taking place.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Quote:Original post by ace_lovegrove
Yeah sorry i forgot to change the -> to . It may be the case that map stores by value on the heap.

In which case i could simply have an array of structs and realloc it to resize it.

Does this make sense?

ace


Since you want to allocate memory yourself, yeah, it does make sense. I don't think malloc will be subject to the problem you,re describing.

structure * ptr = malloc(sizeof(structure) * NUM_STRUCTURE);

...

realloc()

...

free()

This topic is closed to new replies.

Advertisement