Why is there a segmentation fault?

There is a code
void *operator new( size_t stAllocateBlock ) throw (std::bad_alloc)
{
 void *point = malloc( stAllocateBlock );

 if( point != NULL )
 BLOCKS[point] = stAllocateBlock;

 return point;
}


But why should one expect when new - crash
June 5th 19 at 21:24
1 answer
June 5th 19 at 21:26
Solution
the map You refer to is called the same new operator.
it turns out You are applying to the object BLOCKS until it is created.
spoiler
#include <cstdio>
#include <cstdlib>
#include <map>
std::map<void*, size_t> BLOCKS;

void* operator new(std::size_t sz) {
 std::printf("global op new called, size = %zu\n",sz);
 void* p = std::malloc(sz);
 //if(p) BLOCKS[p] = sz;
 return p;
}
void operator delete(void* ptr) noexcept
{
 std::puts("global op delete called");
std::free(ptr);
}
int main() {
 int* p1 = new int;
 delete p1;
}</void*,></map></cstdlib></cstdio>


global op new called, size = 48
global op new called, size = 4
global op delete called
global op delete called
Then.there is a map you can't use the new operator? - Kelsi43 commented on June 5th 19 at 21:29
in global no. - Camila_Leffler commented on June 5th 19 at 21:32

Find more questions by tags C++