I read in article about Smart Pointers in Boost that :
"scoped_ptr is good for raw pointers, while scoped_array is useful for dynamic arrays."
But I didn't get what they mean by "Raw pointers", so neither the sentence.
Could anyone of you explain to me this? Thanks
Raw pointer is a pointer introduced by language syntax with
*
:This is a normal POD ("raw" numeric address in memory) variable, so it destructs trivially. What's important and annoying about raw pointers is:
because of the fact that destruction of
ptr
is a no-op,delete
won't be called and you have to do it manually. Smart pointers wrap around raw pointers and destroy objects pointed to (when necessary to do so).C++11 introduced smart pointers into the standard library in forms of
unique_ptr
andshared_ptr
. You should use those instead of boost ones when targetting C++11.The mention of arrays comes from the fact that we can represent an array as a pointer to the first element and some notion of size. For example,
char*
is a pointer to the first character in the array, and we know that it ends at'\0'
. Dynamic allocations of such arrays are done bynew[]
operator (and destructions bydelete[]
); smart pointers from standard library can handle those types too, but in general use ofstd::vector
is advised instead.