0

So from the top-voted answer to this question, it appears that using raw pointers for storage isn't frowned upon so much. However, then what is the point of a std::weak_ptr? I thought that storage was the exact purpose of that class. If so, is there any circumstance in which a raw pointer is considered good, valid code? If there isn't any, what is the point of having them in the language? Is it simply historical artifact?

5
  • 1
    For the point of having them in the language: yes, it's historical artifact. You must be able to write C and compile it with a C++ compiler. Commented Jun 16, 2014 at 12:33
  • Then the new and delete keywords are completely useless, aren't they? It's not C code, and if pure C++ shouldn't have raw pointers, then...
    – wolfPack88
    Commented Jun 16, 2014 at 12:34
  • 1
    new and delete are historical artifacts of C++, not C. New C++ compilers must still be able to compile old C++ code... Yeah. Commented Jun 16, 2014 at 12:42
  • 3
    new and delete are NOT historical artifacts of C++. For example, to write std::unique_ptr, you will need them. Thus, they are not obsolete. I have no idea why people feel that low level constructs are obsoleted by high level constructs for all use cases. It's sad. Commented Jun 16, 2014 at 17:02
  • 2
    @ThomasEding it's because blogs are given far too much weight in this industry. Someone manages to solve problem X without using language feature Y, and so decide Y should never be used. These are the same idiots who make everything a shared_ptr.
    – James
    Commented Jun 16, 2014 at 20:52

2 Answers 2

4

weak_ptr is solely usable in conjunction with shared_ptr. It also has a little memory overhead while some weak pointers remain (a struct on the heap holding the strong and weak refcounts).

It is fine to use raw pointers if you can assure yourself of the relative lifetimes of the pointer vs. what it points to and prevent dereferencing the pointer while it dangles.

The smart pointers are tools to help with that (under the hood they use raw pointers).

For example unique_ptr sets the lifetime of the pointed to object the same as the pointer with extra functionality for explicit reset or release. The memory footprint for a unique_ptr is the same as a raw pointer unless you use a stateful deleter.

1

A weak_ptr can actually extend the lifetime of a shared_ptr.

To access what a weak_ptr points to, you have to call lock() on it. This temporarily upgrades it to a shared_ptr. You can then use this pointer without the danger of it going away while you are using it. You don't get that guarantee when you are using naked pointers.

new, delete and raw pointers are not completely useless. These are the building blocks of what smart pointers are built upon!

1
  • 1
    technically lock creates a new shared_ptr while the original weak_ptr remains untouched Commented Jun 16, 2014 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.