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

## 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 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 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 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 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 on other sites
Quote:
 Original post by ace_lovegroveYeah 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()

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5
gaxio
11

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631766
• Total Posts
3002215
×