Why should I call?

Possible duplicates:
When to use "new", and when not, in C ++?
When should I use a new keyword in C ++?

It seems I could program something without using the word new , and I never have to worry about deleting anything, so why should I call it?

From what I understand, this is because I would run out of stack memory.

It is right? I guess my main question is: when should I call new ?

+3
source share
6 answers

This is a matter of the lifetime of an object: if you create a stack for your objects, object destructors will be called when these objects go out of scope (for example, at the end of the method). This means that if you pass these objects from the method that created them, you will find yourself with pointers to memory that can be overwritten at any time.

+8
source

This is because at compile time you cannot know if you need an object or how many or what type. The new operator allows you to dynamically distribute objects without thinking in advance.

Here is an example of how to not know the type of an object in advance:

 class Account { ... }; class CheckingAccount : public Account { ... }; class VisaAccount : public Account { ... }; ... Account *acct = type == "checking" ? new CheckingAccount : new VisaAccount; 
+5
source

The main reason you will need to use new / delete is to manually control the lifetime of your objects.

Other reasons are already provided by others, but I think this is more important. Using the stack, you know exactly the lifetime of your objects. But if you want the object to be destroyed only after the event occurs, it cannot be automatically detected.

+4
source

The lifetime of data / objects created on the stack is limited by the block. You cannot return links / pointers to it. To make data accessible through functions, you can create it on the heap with new . Of course, it can be at a higher level or even global. But you rarely know at compile time how much data / how many objects are needed at runtime.

+1
source

You can write many non-trivial programs without naming "new" ones. (or, therefore, delete).

What you would not be able to do (at least without writing or using your own equivalents) is to decide what type of objects or how many you want to create at run time, so you would limit yours. / P>

0
source

[updated]

You can use new to create a new instance of a class or allocate memory (for example, for an array), for example

Object o = new object ();

Before creating a new instance of the Object class, you cannot use it. (If you do not have static methods.) (This is just one use case, sometimes other objects will create or destroy the objects you need / don't need)

There are many good answers here, but it’s hard to explain everything about the new in one answer to SO, and if you don’t understand what happens when new is called, then it’s hard to know when to use it. This is one of the most important areas of programming, therefore, after reading the basic information here, you should study it in more detail. Here is one of the possible articles in which you can start your research:

http://en.wikipedia.org/wiki/New_%28C%2B%2B%29

Topics that you will need to study in order to understand what happens when you invoke a new one, so that you can understand when to call it (most likely this is what I can think of now): - constructors (and destructors) - static classes and methods ...

-2
source

Source: https://habr.com/ru/post/1308169/


All Articles