Why is new operator not used in C++ intermediate for instance object creation ? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Why is new operator not used in C++ intermediate for instance object creation ?

The above scenario can result in memory leaks which can cause a system to crash.

31st Mar 2023, 11:54 AM
Sanjay Kamath
Sanjay Kamath - avatar
5 Réponses
+ 4
Which chapter Sanjay Kamath? Let us also check it out ...
31st Mar 2023, 12:47 PM
Ipang
+ 1
1st Apr 2023, 1:28 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
Ipang when pointers are used the memory on the stack is first allocated and then inititialized using new operator. This protects against memory leaks. It uses OOPS which prevents system crashes, and helps deallocation of the memory. This was carried forward into Higher Level Langages like C#, which was itself inspired by Java. Garbage collection is implemented on the HEAP in C#. GC.Collect() new keyword is typically used for instance objects. Static objects for static classes do not require the new keyword. I followed up on your method which is what google recommends. However to avoid stack overflow errors, and, to ensure good memory management, and, for cross compatibility it is better to use new keyword. Using my personal experience as a DBA I would like to cite my fav object - The stored procedure. It is encapsulated, pre-compiled and optimized for the current DB load.
1st Apr 2023, 2:58 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
OOPS objects should be used where available, because they are more secure than unmanaged code. This is why oops objects like"new" are used The MOST IMPORTANT thing here is that using the 🆕 keyword gives updatable memory locations, and, hence better use of memory resources.
2nd Apr 2023, 8:52 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
Sanjay Kamath This page covers about storage duration. Scroll down till you find "Storage duration" heading, and you can see there's explanation about difference between automatic & dynamic storage. Objects created in stack (automatic storage) are not instantiated with `new`operator. Objects created in heap (dynamic storage) are instantiated with `new` operator. That is to refer how memory is allocated for storing the said object(s). https://en.cppreference.com/w/cpp/language/storage_duration I hope I was not mistaken the concept : )
1st Apr 2023, 11:33 AM
Ipang