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.