Sign in to follow this  
dave

This me being dumb?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Some part of the C++ runtime library is using operator new and running before initialization of the global variable entries is actually taking place.

Share this post


Link to post
Share on other sites
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()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this