0

Everywhere I go for tutorials, I see that you have to setup a world in box2d like this:

b2World* world;
world = new b2World(b2Vec2(0.0f, -9));

and why are they not just doing this:

b2World world;
world = b2World(b2Vec2(0.0f, -9));

I mean, I do not see advantages of doing this - So could someone please try to explain this? Thanks! Your time is much appreciated :)

  • 1
    You cannot do the second one because `b2World` [has no default constructor](http://www.learn-cocos2d.com/api-ref/1.0/Box2D/html/classb2_world.html). You can probably do `b2World world({0.0f, -9});` instead and it would be more efficient and harder to get memory management wrong, but the point of Box2D is to make realistic physics simulations, not to write pretty code. – nwp Feb 05 '17 at 15:35
  • Thanks! I think I get what you are saying but why did the developers of Box2d base every on pointers to the world and not just a b2world without a pointer? –  Feb 05 '17 at 15:52
  • 1
    @SUhost Unless the object's lifetime needs to survive the scope of the declaration, unfortunately, this is more of a kneejerk reaction to use `new` whenever the object being created is complex in nature. Other reasons could be that the writers of the sample code are Java or C# programmers, and `new` to create objects is a habit. – PaulMcKenzie Feb 05 '17 at 16:11
  • You misunderstand. You can use either, but you must use it correctly. – Kenny Ostrom Feb 05 '17 at 17:19
  • @KennyOstrom but why do probably everybody out there then use the new operator? :/ –  Feb 05 '17 at 17:47
  • @PaulMcKenzie So what your saying is that there actually is not really any technical explanition for why they are all using the new operator when creating the world but a kneejerk reaction? Thanks again –  Feb 05 '17 at 17:49
  • 2
    @SUhost -- Well, the only reason for using `new` in C++ in this case was if the object needed to manage its own lifetime, or if the underlying API requires the object to be "alive", outside of the scope where it was declared. Otherwise, yes, it is kneejerk (or force-of-habit) coding by C++ programmers that equate "complex objects" with "dynamic creation". – PaulMcKenzie Feb 05 '17 at 17:53
  • The portion of the code you posted looks like bad practice in modern c++; however, the portion of the code you did not post illustrates that they were focusing on NOT making unnecessary copies of large objects. Of course, it should be a reference or some sort of smart/unique pointer. – Kenny Ostrom Feb 05 '17 at 22:45
  • @KennyOstrom, you said "NOT making unnecessary copies of large objects. Of course, it should be a reference or some sort of smart/unique pointer. " but if you declared the b2world like this `b2World world;` as a class variable, then I cannot see why there should be generated unnecessary copies? Could you please explain? Thanks, much appreciated buddy! –  Feb 06 '17 at 15:17
  • If you pass that to a function like "void ChangeTheWorld(b2World *pWorld)" then it only has to copy a pointer. – Kenny Ostrom Feb 06 '17 at 16:27
  • And is that more more efficient when it comes to memory? –  Feb 06 '17 at 18:12
  • @KennyOstrom, would that be the main reason? –  Feb 07 '17 at 15:39
  • note that you can and should use `auto world = make_unique(b2Vec(0,10));`, then you can't accidentally forget to delete it – M.M Feb 08 '17 at 23:47
  • JFYI, I voted up this question, because I think it's a question that lots of people may have wondered about at one time or another. – Louis Langholtz Feb 08 '17 at 23:51

1 Answers1

1

You don't have to use the new operator. As you noted, you can instantiate it on the stack as:

b2World world(b2Vec2(0, 10));

Did you take a look at how big the b2World instances are though?

Try something like:

std::cout << sizeof(b2World) << std::endl;

I get a size of 103,296 bytes!

For many systems, putting 103,296 bytes onto the stack is no problem. That's not the case for all systems however. And running out of stack space isn't always a very obvious situation.

In any case, I find the size of b2World to be surprisingly large (and in my fork of Box2D I've reworked the world class to be much smaller). So this question boils down to the more general question of whether to put the world instance on the stack or to put it on the heap.

I found the following question and answers which I believe can provide further guidance: When is it best to use the stack instead of the heap and vice versa? The second highest voted answer was:

As a rule of thumb, avoid creating huge objects on the stack.

While I don't know for certain that the size of b2World instances was why examples use new (even when they could have used the stack), I suspect it had much to do with it.

Community
  • 1
  • 1
Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40