Using source pointers in modern C ++ post C ++ 11

What are the main reasons for using raw pointers in 2014, given that the C ++ 11 standard is now well supported by most decent compilers?

I defined a couple of scenarios:

  • You are expanding on an outdated code base that makes heavy use of source pointers, and you want to maintain consistency in style.

  • You are using a library that only exports source pointers, but I think you can use casts anyway.

  • You want to use pointers to provide several levels of indirection. (I don't know, C ++ 11 is enough to know if this can be done using smart pointers or using some other methods.)

What other scenarios do you think are suitable for using pointers?

Would you even recommend learning about pointers in general today?

+7
c ++ c pointers c ++ 11
source share
6 answers

I can imagine the circumstances in which you have a statically allocated array and you want to use a raw pointer to iterate through it in high-performance code. There is nothing wrong.

Your # 1 is true.

Your # 2 may not be right: if you use β€œcasts” to convert source pointers belonging to a third-party library into smart pointers (meaning local property), then something went horribly wrong.

Your # 3 is technically right, but avoid it whenever you can.

Currently, it is not recommended to play with raw pointers to your dynamically allocated memory. That is, the advice is to avoid new without smart pointers (and the consequence is that you do not need to delete ).

+12
source share

Everything is against the original pointers, because they are too easy to leak.

But you can use raw pointers to point to data located elsewhere ... just don't new / delete them. To do this, use std::unique_ptr or std::shared_ptr . Or for dumb (POD) memory buffers, use std::vector<unsigned char> , not malloc and free yourself.

Think of std::vector<heavy_object*> when you need to juggle with sub-selections of objects that are non-trivial to copy but already exist elsewhere. You need pointers for this.

You will also need pointers in functions for optional arguments, where links are not cut because you want to pass nullptr .

Pointers to consecutive objects can also be easily iterated without any std::iterator overhead . Just ++ or -- and what is it. I often use direct pointer iteration instead of begin , end for vectors.

As you understand how they work ... you will need a lot of them, and you will use them correctly.

+8
source share

Smart pointers are used to handle ownership problems, but not all pointers are used to manage objects. For example, it makes sense to pass a raw pointer to a function if you do not plan to transfer ownership of this function (i.e. you just want the function to process the data addressed by the pointer)

+6
source share

IMHO Source pointers still have their place.

What C ++ 11 gives us is the ability to manage the life cycle of raw pointers, so we don’t need to delete them ourselves.

There is nothing wrong with using raw pointers if they are managed by the smart pointer / pointer manager in the right area or frame to ensure their lifespan. If this is true, you will never have to delete the raw pointer, and you can safely use them within the frame / frame, during which their service life is guaranteed.

I would say, if possible, save the original pointer to your new object in std::unique_ptr , if its duration should be controlled by this area / frame. Once this is done, use the raw pointer inside this area frame. Just never delete it;

Sometimes it is impossible to control the lifespan of a new object from one area or frame. In this case, use std::shared_ptr in each area / frame, which independently should control the lifespan of the new object. Then, within each region / frame, there is no reason not to use a raw pointer, as if it were controlled using std::unique_ptr .

Thus, often there are no reasons that entail a lack of speed of smart pointers, since one of their strengths is to manage the life cycle of a new object to ensure the authenticity of the original pointer and the automatic destruction of its object when it is no longer needed.

There are other cases where a raw pointer does not fit.

For example, when a managed pointer needs to transfer "ownership" to another area / frame. That's when you need the area / frame responsible for managing the life cycle of the new object for the change. In these cases, avoid raw pointers such as the plague!

+3
source share

What other scenarios do you think are suitable for using pointers?

One of the main use cases for source pointers is when you have pointers without permissions. Typically, if the link will work, but you want to avoid the limitations of the link (unrealized, not copied). You can use the reference_wrapper type in these cases, but it's easier to just use a raw pointer instead. Smart pointers encode the ownership right (who creates and destroys the object), therefore, if there is no ownership right for encoding (because it is implied otherwise), then the raw pointer is in order.

To make this clear, typical examples of what I just explained are things like:

  • temporary copied functors that need a pointer to some object that it does not have.
  • internal cross-references in the data structure (for example, "back pointers").

But it’s important to note that these things should not be present in interfaces at all. As a rule, you can completely avoid the use of raw pointers in interfaces (for example, library functions and classes) and use them exclusively inside, that is, in library code, and not in user code. In other words, if you need to use raw pointers, hide them.

Optional pointers are also sometimes visible for optional function parameters, where you can pass nullptr if you don't want this result.

The main thing to avoid and which can be avoided at all is the bare call of new / delete in user code. A typical good modern C ++ library (and even more so with C ++ 11) will not have such exposed new / delete entries and that is a fact.

Source pointers themselves are not a problem, which is problematic: (1) manual memory management and (2) property management (which is problematic if source pointers are used instead of smart pointers).

Would you even recommend learning about pointers in general today?

Of course, you should learn about pointers. They are necessary for understanding programming and learning to write code on the library side. The source pointers are still very present in the guts of a lot of library code, etc., even if you don't see them.

+1
source share

common reasons for using source pointers from the top of the head.

  • Reading and parsing binary files
  • Direct interaction with equipment
  • Arrays?
  • Speed. AFAIK smart pointers are slower than source pointers, and there are relatively safe ways to use raw pointers. See, for example, the chrome code base or WebKit. Both use different kinds of traceable memory without the full overhead of smart pointers. Of course, with limitations.
-one
source share

All Articles