Difference between creating an object with or without new operators

Hello, I am asking a question here for the first time. So, I hope would like to help me.
I just want to know the all differences between creating an object with or without "new" opertor?
Can anyone help?
Any reply would be highlt appreciated!
Thanks!
An object created by new has dynamic storage duration, meaning that the lifetime of the object is controlled by the programmer. In other words, things that get new'd don't get destroyed at the close-of-scope as is the case for variables with automatic storage duration (objects created w/o new), and won't even be destroyed as a result of normal program termination, as is the case for objects with static storage duration.

Additionally, memory allocated by new expressions can have a size which is determined at runtime. In other words, you can't create an array of x objects unless x is a constant expression, but you can with new.

This is nice -- using new, you can store arbitrary numbers of objects (within memory constraints) and pass references to those objects around your program however or whenever you choose.

This is a terrible power, though, since the rest of the answer explains why you shouldn't do this except when you have to.

The programmer is in control of when the object gets destroyed by calling delete on the result of the new-expression. Don't forget, since failing to delete the object causes a memory leak, which is a serious error - memory occupied by the object is never given back to the system, which can result in anything from catastrophic system failure (although this is unlikely on a modern desktop computer) to performance issues (as incredibly-slow swap memory is consumed) that can only be resolved by a reboot.

That's worse because very often a delete expression will appear far away from the corresponding new.

Some modern systems incorporate an out-of-memory (OOM) killer, which is responsible for instantly crashing programs it thinks are the cause of the OOM condition. If that happens, such a system will probably reclaim the resources your program used, which avoids complete system failure.

In some cases, the OOM-killer might guess wrongly, and crash the wrong program.

new-expressions can always fail, even for POD or primitive types (there's no guarantee system memory is available, and once you're out, bad things happen). Automatic or static memory allocation will not fail, although the real world occasionally says otherwise.

Placement-new expressions let you construct objects at a particular location in memory. Picking the wrong spot causes your program to exhibit undefined behavior. If this variant of new appears, the destructor of the created object needs to be called explicitly - which never occurs any other time. Failure to destroy the created object can cause code not to run, and probably a resource leak :(.

If you use new or new[] but use the wrong corresponding delete delete vs delete[], your program exhibits undefined behavior.

All of these dangers are in addition to the dangers inherent in the use of pointers, which new expressions return.

Bottom line: never write a new expression when when dynamic memory isn't required. When it is, strongly prefer standard containers or smart pointers. Outsource your resource (not just memory) management to the standard library as often as you can. Standard utilities make the problem much less error-prone.
Very much thanks to mbozzi. I am highly thankful to you for this detailed answer.
Last edited on
Topic archived. No new replies allowed.