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

Author Topic: My first SFML Program  (Read 6209 times)

0 Members and 1 Guest are viewing this topic.

Evan Bowman

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: My first SFML Program
« Reply #15 on: August 15, 2016, 05:54:04 pm »
Quote
Pushing the actual sprites would be my choice though; they're tiny and most likely don't need to be on the heap.

The header information of a vector is stored on the stack, while the elements that you push back are still allocated in free store (i.e. on the heap). But of course using a vector is way better than performing heap allocations for single objects with operator new.

Quote
There doesn't seem to be any benefit to using std::array instead of std::vector here. Both are equivalent really with vector having more flexibility if something changes.

True, std::vector is indisputably more flexible, the only reason I brought it up is that using an array performs allocation for the contained elements all at once upon construction, while using std::vector::push_back(const & value_type) calls additional copy constructors for each call to push_back(), and you take a performance hit each time vector doubles in size upon reallocation. If you know before hand that you're pushing back at least three elements and are using a vector for future flexibility, it might be worth calling the fill constructor rather than calling push_back() three times, or if you do want to call push_back() first calling std::vector::reserve().
« Last Edit: August 15, 2016, 05:56:31 pm by Evan Bowman »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: My first SFML Program
« Reply #16 on: August 15, 2016, 07:48:57 pm »
The header information of a vector is stored on the stack, while the elements that you push back are still allocated in free store (i.e. on the heap).
Good point. I do forget that sometimes but it was mainly because I use the stack for actual Sprite objects too because they're tiny. It's nice that vector take care of this stuff, though; memory management is boring  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Evan Bowman

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
    • Email
Re: My first SFML Program
« Reply #17 on: August 15, 2016, 09:36:54 pm »
memory management is boring  ;D

Subjective  :)

 

anything