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

Author Topic: Using the stack or the heap for SFML objects  (Read 3274 times)

0 Members and 1 Guest are viewing this topic.

rbkza

  • Newbie
  • *
  • Posts: 4
    • View Profile
Using the stack or the heap for SFML objects
« on: April 12, 2013, 12:25:44 am »
All the example code I've seen for SFML creates objects such as textures and sprites as follows:

Code: [Select]
sf::Texture texture;
texture.loadFromFile("texture.png"))
sf::Sprite sprite;
sprite.setTexture(texture);

I don't see the new keyword anywhere here, so I'm assuming this means the objects are created on the stack, and not on the heap? Surely this is a bad idea, since with a decent size codebase you'll eventually exceed the stack size and get stack overflow issues?

So my question is this: should I create new objects on the heap or on the stack? Or is SFML internally managing memory allocation in such a way that the stack isn't actually used?
« Last Edit: April 12, 2013, 12:27:35 am by rbkza »

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: Using the stack or the heap for SFML objects
« Reply #1 on: April 12, 2013, 12:35:40 am »
A rule of thumb for all C++ programmers: Never use pointers if you don't have to! :D Stack allocation is much faster than heap allocation because all it does is move the stack pointer. I'm presuming that you don't want to have more headaches than necessary, so why use pointers unnecessarily? Adding pointers to the equation adds complexity to a rather simple task.
Current Projects:
Technoport

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Using the stack or the heap for SFML objects
« Reply #2 on: April 12, 2013, 12:48:30 am »
The stack has a limit yes, but if you just use a few objects, then there won't be a problem and as soon as you go a bit bigger, then you'll probably need to use some dynamic allocations anyways (e.g. reading required objects from a file at runtime).
sf::Texture doesn't get stored on the stack, but it's just a handle for the texture that actually lives on the GPU RAM and sf::Sprite is a very light object, so you will be able to construct a lot of them until you hit the stack size limit.
Besides all that, if you use a good structure, you'll be using STL containers, which allocate their content on the heap, thus if you allocate a std::vector<sf::Sprite> nearly no stack memory is wasted. Also if you declare them as normal class members and then maybe later on construct that class dynamically, all the "stack" objects will infact get allocated on the heap.

Thus as The Terminator said, try to avoid the heap and if you do use it, make sure you use smart pointers instead of raw pointers.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/