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

Author Topic: Isn't letting a program create a new sf::Event instance every loop useless?  (Read 6100 times)

0 Members and 1 Guest are viewing this topic.

Superpelican

  • Newbie
  • *
  • Posts: 8
    • View Profile
Hello everyone,

I'm a beginning C++/SFML programmer and I'm using Linux. When reading the wiki' s I came across this:
while(App.Isopened()){
 sf::Event Event;
}
 

Isn't it useless to create a new instance of sf::event at every loop? Isn't that a waste of memory and CPU? Please correct me if I'm wrong and please explain why/the inner workings of this code. Please remember I'm a beginning programmer and I'm not trying to insult or offend anybody. I'm just curious why "sf::Event event;" isn't placed outside of the While loop (I didn't post all code that' s inside of the While loop, but only the relevant piece).

Kind Regards,

Superpelican
Manjaro Linux KDE 0.8.3 - GCC 4.7.2 x86_64 - KDE 4.10 - Kate Text Editor (dev environment)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Isn't that a waste of memory and CPU?
No, the overhead for an sf::Event object on the stack is absolutely negligible, even more since it's a POD (plain old data) type. Declaring variables as late and as local as possible is much more important, since it makes code more readable (one knows the scope of the variable) and avoids accidental accesses to a variable (before/after its usage). Such micro-optimizations usually lead to nothing as long as other parts of the code determine the performance, the phenomenon is also known as premature optimization.

By the way, it's probably a bad idea to begin with C++ and SFML at the same time, since SFML requires already solid knowledge of the programming language. And you will have a lot of questions like that if you don't know C++ well ;)
« Last Edit: February 19, 2013, 07:12:38 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
On a more technical side, here is what happens exactly.

First, the variable is allocated on the stack, and a stack allocation is a no-op (well, actually it's a pointer addition); the stack has a fixed size and is already allocated when the program starts, and the address of each object declared on the stack is pre-computed by the compiler upon compilation.

Secondly, it's a POD structure: it has only public POD members and a trivial constructor, therefore its construction is - again - a no-op.

As a consequence, declaring it inside or outside the loop doesn't make any difference at all, since both its allocation and construction are empty operations. There's just some space pre-allocated for a sf::Event variable somewhere on the stack, which is ready to be used when the variable is used later in the code.
« Last Edit: February 19, 2013, 07:59:32 pm by Laurent »
Laurent Gomila - SFML developer

Superpelican

  • Newbie
  • *
  • Posts: 8
    • View Profile
First of all: thanks all for spending some of your time to answer my question. Now I understand why the code was written the way it is. And why it doesn't matter that a new instance is created on every loop (the RAM memory is claimed by the program anyway, so it doesn't really matter whether it's being used by some variable or not).

By the way, it's probably a bad idea to begin with C++ and SFML at the same time, since SFML requires already solid knowledge of the programming language. And you will have a lot of questions like that if you don't know C++ well ;)
Well I've already read http://www.cplusplus.com/doc/tutorial/ and understand everything (well except the template part). The only problem really is that I tend to forget it. And that's exactly why I'm trying to learn SFML: I eventually want to create a very simple 2d game with SFML, so I can use the knowledge I picked up by reading the cplusplus.com tutorial in a fun way. Which (hopefully) helps me at remembering the essential information.  :)
Manjaro Linux KDE 0.8.3 - GCC 4.7.2 x86_64 - KDE 4.10 - Kate Text Editor (dev environment)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Online tutorials cover only the surface, often they omit important background knowledge. Be aware that if you save time now at learning, you will later have more problems, and eventually waste more time.

You should definitely consider learning C++ with a book, here are some possibilities listed. Thinking in C++ is available for free as an e-book.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything