I am fully aware that if I want to create a smart pointer to an array the best way is to use
boost::shared_array<T>( new T[20] );
What I don’t understand is the crash I have when a shared pointer of this type gets out of scope.
boost::shared_ptr<T>( new T[20] );
I always thought that the code above would create a leak since it calls delete on the first element of the array by leaving untouched the others but this is not the case and I have a segmentation fault.
Can you help me understanding why? I have worst behavior if I don’t use the shared pointer but a plain vanilla pointer
CMyClass *p = new CMyCLass[10];
///....do stuff
delete p;
*** glibc detected *** ./test: munmap_chunk(): invalid pointer: 0x088eb00c ***
Because your code has an Undefined Behavior.
You need to use
delete [].newyou must usedelete.new []you must usedelete []Doing otherwise leads to Undefined Behavior. And one its Undefined Behavior then your program might show any behavior.It might crash or work or whatever(literally) so all safe bets are off.
For
shared_ptryou should use your own custom deletor function to deallocate appropriately. In case of array allocation usingnew [], you need to usedelete []. Something like: