Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: When should I be using pointers?  (Read 2961 times)

0 Members and 1 Guest are viewing this topic.

codelyoko373

  • Newbie
  • *
  • Posts: 8
    • View Profile
When should I be using pointers?
« on: August 22, 2017, 04:35:35 am »
(Sorry if this is in the wrong place)

I firstly want to point out I know what pointers are and how to use them though they're still quite a confusing topic to me. I can see how they're useful when you're for example wanting to pass a variable around through functions without making copies of it but that's about it.

Should I be making all variables pointers or not for example?

The reason I mainly ask this is because the tutorial series I watched for making pong in sfml, the guy basically always created pointers, all his variables were pointers so I've done exactly the same thing but it doesn't feel right.

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: When should I be using pointers?
« Reply #1 on: August 22, 2017, 09:36:05 am »
Firstly, a bit of theory:
https://stackoverflow.com/a/79936

Now, If the stack model is convenient for you (small size, short lifespan, etc..), use it, there's no reason not to (however,make sure you're not creating global variables just so you can use stack model. Always avoid globals). Only use heap if stack memory management is insufficient.

On top of that, when you finally use pointers, use smart pointers instead of the raw ones, as smarts are safer.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: When should I be using pointers?
« Reply #2 on: August 22, 2017, 01:32:24 pm »
Raw pointers should in pretty much all cases be avoided.
If you want to pass a variable without making a copy, use a reference.
If you want to dynamically allocate an object, use a smart pointer (i.e. std::unique_ptr).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

codelyoko373

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: When should I be using pointers?
« Reply #3 on: August 22, 2017, 10:18:56 pm »
Firstly, a bit of theory:
https://stackoverflow.com/a/79936

Now, If the stack model is convenient for you (small size, short lifespan, etc..), use it, there's no reason not to (however,make sure you're not creating global variables just so you can use stack model. Always avoid globals). Only use heap if stack memory management is insufficient.

On top of that, when you finally use pointers, use smart pointers instead of the raw ones, as smarts are safer.

So I'm guessing I shouldn't be making all those pointers then? (Shown in the screenshot)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: When should I be using pointers?
« Reply #4 on: August 24, 2017, 07:01:34 pm »
So I'm guessing I shouldn't be making all those pointers then? (Shown in the screenshot)
No. This should not be your default way of storing objects. For a lot of projects, you can just create the objects directly on the stack (sf::Text title;) but for larger projects with an extreme amount of objects and large objects that you do need on the heap (e.g. resources), use smart pointers to store them (std::unique_ptr<sf::Font> font;).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything