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

Popular posts from this blog

hibernate - How to load global settings frequently used in application in Java -

python 3.x - Mapping specific letters onto a list of words -

objective c - Ownership modifiers with manual reference counting -