Jump to content
  • Advertisement
Sign in to follow this  
dave

This me being dumb?

This topic is 4804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!