#What is the Point of Pointers?

28 messages · Page 1 of 1 (latest)

mossy bone
#

Hi all,

I am trying to learn C++, and am trying to figure out the point of pointers.
I understand the need for the heap for large objects as the stack is not large enough, as well as the need of pointers for certian data structures like linked lists.

What I can't understand is the need for them outside of this, and I think Joseph Mansfield explains this perfectly: https://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself#answer-22146244

I just can't figure out the need, especially with the argument of outliving a scope when an object will be in scope if it is in the main function.

Because I can't figure out the need for pointers, I also, by extension, don't understand the need of smart pointers, although I know that they are important for some reason.

If anyone can help that would be great!

shell palmBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

sage inlet
#

A smart pointer is just a pointer that handles it's own deallocation once it leaves scope. Knowing that, the question just becomes why do you need pointers at all

#

C++ actually attempts to reduce your interaction with pointers by using references instead. In my C++ projects, the only times I ever use raw pointers is to interact with C code

#

Do you understand what references are and why you would need those? A pointer would be useful for most of those same things.

mossy bone
#

@sage inlet I understand what references are and passing by reference, however I can't see the point of using a pointer instead of a reference operator and, from coming from garbage collected languages like Java, JS, and Python, I can understand passing by reference for large objects into a function as a performance thing, and that the heap is larger than the stack so large data should be stored in the heap, however I can't understand any other use case. I guess smart pointers would be valid for the large data, but I would assume only the unique smart pointer would be needed for that if you keep the file in the main function?

#

@sage inlet Therefore I can't see the need for the other smart pointers?

copper salmon
# mossy bone Hi all, I am trying to learn C++, and am trying to figure out the point of poin...

I'm not 100% sure what your question is about
you linked an SO post, are you mostly agreeing with what Joseph Mansfield explains in his answer except for the "outlive the scope", or are you mostly disagreeing/not seeing the point, especially with the "outlive the scope" and except "large object"?
the overall tone of that SO answer is that you only use pointers when you have no other options/no better suited options, to achieve the desired effect

I don't think you're saying that pointers are completely useless, since you acknowledged some of their uses, is your question somewhat of an "when should I use pointers?" and "when do the examples that Joseph Mansfield lists actually come up useful?", kind of question?

anyway since I'm not sure about what you're asking, I will address the part I'm sure you're mystified by: the need to have objects outlive the scope in which they are created
in fact I'm not gonna say much except point out that you have yourself provided a perfect use case for that: linked lists
the typical linked list has dynamically allocated nodes that point to one another
typically when you end up reaching out to a linked list, you rarely know in advance how many nodes you need, or what the nodes' content will be
so later down the line when you end up needing to create a node, you typically need the created node to outlive the scope in which it is created, so that the data structure will properly point to a valid node

especially with the argument of outliving a scope when an object will be in scope if it is in the main function
there are plenty of reasons why it would awkward or undesirable to put objects that need to live for a long time in the main function, you certainly rarely can (or want to) create all the nodes in a linked list inside of main

mossy bone
#

@copper salmon I think I agree with what Joseph is saying. I also understand that pointers are needed for a lot of data structures and understand that. I think you are right in that I don't know what I am asking because I truly am confused. I think my main questions are:

  • Is Joseph's assumption correct
  • Apart from with allocating data to the heap due to stack memory restrictions, and data structures, when should pointers be used
  • By extension, when should smart pointers be used?
copper salmon
# mossy bone <@374795910032523266> I understand what references are and passing by reference,...

however I can't see the point of using a pointer instead of a reference operator
reference isn't really an "operator", but setting that aside, in that specific case you would favour the use of references
in C however, references do not exist, so you are forced into using a pointer for these situations. If, as SWR mentioned, you have to interact with C code from C++, you often end up needing to adapt to the C interface. I think that point was also brought up in the SO answer

about "unique smart pointer" vs the other smart
std::unique_ptr is for unique ownership
std::shared_ptr is for shared ownership
both can validly be used to "deal" with large object you don't want on the heap, the difference comes from, well, unique vs shared ownership
I'll drop this for now cause you've just reacted to my previous message

#

well now you're forcing me to actually read the SO answer in some details instead of just skimming it

#

Is Joseph's assumption correct
well depends what you consider to be an "assumption" cause he explains many things and give some broad recommendations/advice, so I guess I'll give you some "commentary" on his answer

The important take-home message is that you should always use the appropriate tool for the job. In almost all situations, there is something more appropriate and safer than performing manual dynamic allocation and/or using raw pointers.
I agree. Problem is then to understand what tool is appropriate for the job, and what properties you actually want/need, to then select the right tool.

You should only use dynamic storage duration when you need it. That is, you should always prefer creating objects with automatic storage duration when you can.
generally agree, but again, how do you know when you need it or not? Coming up with a good and sane program structure is hard

The main two situations in which you might require dynamic allocation:

  • You need the object to outlive the current scope
  • You need to allocate a lot of memory
    mostly agree, those are the first two cases that would come to mind. Note that this is immediately followed by some tools you would use to achieve dynamic allocation without the use of new/delete.
    One more situation I would have mentioned explicitly would be: you do not know how much memory you need (when you write the code). That's arguably a cousin to "you need to allocate a lot of memory", because you possibly need a lot of memory when you do not know how much you need :)
    Anyway, in those contexts you'd normally reach out to containers like std::vector, which handle and perform the dynamic allocation for you. Depending on the exact kind of properties you need, you would use different type of container
#

on raw pointer usage, I agree with his explanations for 1 and 2, what he says for 3 is also correct, 4 is fine though I don't think you actually understand what he means there, and having a proper discussion about that would take quite a lot of time I think, 5 we've already talked about it

and now that I've read through this I don't think I can come up with other use cases easily

#

Apart from with allocating data to the heap due to stack memory restrictions, and data structures, when should pointers be used

well to quote myself

you only use pointers when you have no other options/no better suited options, to achieve the desired effect
which honestly is just a paraphrasing of the SO post, because the SO post you linked starts with
In almost all situations, there is something more appropriate and safer than performing manual dynamic allocation and/or using raw pointers.

#

if you wanted a simple list of use cases where you really really should use raw pointers, there isn't one

#

"modern" C++ gives you tools to avoid having to use raw pointers in "almost all situations", a lot of the point is to avoid having to use raw pointers, because there are better options

#

"references" is an example of better option (if they are applicable)
proper data structures, is an example of better option (if they are applicable)
smart pointers, are an example of better option (if they are applicable)
optional is an example of option (if it is applicable)

if the above didn't exist, you'd have more places where you would be forced to use raw pointers
but the above exist, and they cover a large range of cases where you would use raw pointers if you didn't have access to those tools, e.g. if you were using C instead of C++

#

also, I forgot to point it out, but "allocating data to the heap due to stack memory restrictions" is a situation where dynamic allocation should be used, which is a different problem than "using raw pointers"

#

that's why the SO post was split into two distinct parts

#

in C, when you are doing dynamic allocation you essentially have to use raw pointers, but in C++ you have other tools that will deal with the dynamic allocation for you, so you do not need raw pointers when you want dynamic allocation

#

when should smart pointers be used?

in the simple cases, smart pointers should be used when you want dynamic allocation (of a fixed number of objects, very often one object)

#

if you feel the need to reach out for new and delete, that's often where you would use a smart pointer

#

for the smart pointer thing, maybe try watching one of the cppcon talks about smart pointers, those are generally good

mossy bone
#

Wow @copper salmon I have been trying for 4 hours to understand the need for pointers and I think you and Joseph have finally made it click. Smart pointers I still feel a bit lost in but I will watch a cppcon talk on it to understand. The only other use case I can think of is with embedded if you need to read a specific register potentially? But I’m not sure if that is accurate for things like arduino’s and esp32’s. Thank you so much for your help

shell palmBOT
#

@mossy bone Has your question been resolved? If so, run !solved :)

lapis yarrow
#

https://www.youtube.com/watch?v=JfmTagWcqoE
My favourite talk on smart pointers

http://CppCon.org

Presentation Slides, PDFs, Source Code and other presenter materials are available at: https://github.com/cppcon/cppcon2016

Lifetime safety means writing code that, by construction, is guaranteed to eliminate two things: (a) use of null/dangling pointers (including pointerlike things such as references, iterators, views, an...

▶ Play video
mossy bone
#

https://youtu.be/YokY6HzLkXs?si=FlLspSiwzwruY7iU I was going to watch this one first but @lapis yarrow I will watch this one after

https://cppcon.org/

Back to Basics: C++ Smart Pointers - David Olsen - CppCon 2022
https://github.com/CppCon/CppCon2022

Smart pointers were one of the many powerful additions to C++11, providing programmers with easy-to-use tools to help manage memory resources and avoid certain kinds of memory errors. This back-to-basics session will giv...

▶ Play video
#

!solved