Question

So I was recently told off on this site for using the new operator but I didn't get a response when I asked why. So my question is: is there actually something bad about using new?

The only reason I can think of is a memory leak when a new call isn't paired with a delete call.

Was it helpful?

Solution

You weren't told off. A comment said:

You should never use new unless there's a good reason to.

This is currently +2 (two upvotes agreeing with the comment).

In the case of your example, using new is optional. You could have just as easily instantiated the object on the stack with:

sf::RenderWindow window(...);

This has the advantage of not needing an explicit delete and no way for the object to escape destruction when it is no longer needed.

On a scale of zero (completely harmless) to ten (destroys lives and property), using new instead is maybe a 1. Still, it can drive some programmers crazy when they see it because of severe childhood early trauma being bitten by this kind of malfeasance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top