Reputation: 331
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
Upvotes: 1
Views: 201
Reputation: 24561
Their wording is unfortunate. By "raw" pointers, people usually mean the primitive pointer types, regardless of what they point to. What they mean is:
scoped_ptr is good for pointers to single objects, while scoped_array is useful for dynamic arrays
Upvotes: 2
Reputation: 39390
Raw pointer is a pointer introduced by language syntax with *
:
int* ptr;
This is a normal POD ("raw" numeric address in memory) variable, so it destructs trivially. What's important and annoying about raw pointers is:
int* ptr = new int(42);
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
and shared_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 by new[]
operator (and destructions by delete[]
); smart pointers from standard library can handle those types too, but in general use of std::vector
is advised instead.
Upvotes: 1