c++ - Strange behavior of custom allocator -
if run program, return sigsegv. if dissasemble , debug, saw crash @ begin of function dcallocator::free()
.
#include <cstdlib> class dcallocator { public: void * alloc(unsigned long size) { return malloc(size); } void free(void * p) { free(p); } }; int main() { dcallocator dalloc; int * arr = (int*)dalloc.alloc(sizeof(int)*40); (int i=0; i<40; ++i) arr[i] = i*2; (int i=0; i<40; ++i) cout << arr[i] << ", "; cout << endl; cout << "end" << endl; dalloc.free(arr); // there sigsegv cout << "of deallocation" << endl; return 0; }
your free()
member call until runs out of stack space. meant call ::free()
:
void dcallocator::free(void* ptr) { ::free(ptr); }
Comments
Post a Comment