Scoped Pointer in Boost : What does mean a raw pointer?

198 views Asked by At

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

2

There are 2 answers

0
Bartek Banachewicz On

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.

0
Nemanja Trifunovic On

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