3

Possible Duplicates:
When to use “new” and when not to, in C++?
When should I use the new keyword in C++?

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

From what I understand, it's because I would run out of stack memory.

Is this correct? I guess my main question is, when should I call new?

Community
  • 1
  • 1
cam
  • 8,095
  • 17
  • 54
  • 81
  • 1
    Dupe http://stackoverflow.com/questions/679571/when-to-use-new-and-when-not-to-in-c –  Apr 27 '10 at 12:34
  • And: http://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c – fretje Apr 27 '10 at 12:39

6 Answers6

9

It's a matter of object lifetime: if you stack-allocate your objects, the objects destructors will be called when these objects go out of scope (say, at the end of the method). This means that if you pass these objects out of the method that created them, you'll find yourself with pointers to memory that could be overwritten at any time.

Guido Domenici
  • 4,828
  • 2
  • 24
  • 38
5

It's because you may not know at compile time whether you need an object, or how many, or of what type. The new operator allows you to dynamically allocate objects without having to know such things in advance.

Here's an example of not knowing the type of object in advance:

class Account { ... };
class CheckingAccount : public Account { ... };
class VisaAccount : public Account { ... };

...

Account *acct = type == "checking" ? new CheckingAccount : new VisaAccount;
Marcelo Cantos
  • 167,268
  • 37
  • 309
  • 353
  • It doesn't allow you to generate objects of types that are determined at runtime. The type must be known in advance. As for "how many", one can also use standard containers. – sellibitze Apr 27 '10 at 12:41
  • @sellibitze, what type of object does the `acct` variable point to at runtime? And how do you think standard containers allocate memory for their elements? – Marcelo Cantos Apr 27 '10 at 13:23
4

The main reason you'll need to use new/delete is to get manual control the lifetime of your objects.

Other reasons are already provided by others but I think it's the more important. Using the stack, you know exatly the lifetime of your objects. But if you want an object to be destroyed only after an event occurs, then it cannot be automatically defined.

Klaim
  • 60,771
  • 31
  • 121
  • 186
1

The lifetime of the data/objects created on the stack is restricted to the block. You cannot return references/pointers to it. For data to be available across functions, you can create it on the heap with new. Of course, it can be at a higher level, or even global. But you seldom know at compile time how much data/how many objects will be needed at run time.

AngryWhenHungry
  • 448
  • 2
  • 14
0

You can write a many non-trivial programs without ever calling "new." (or thus delete).

What you wouldn't be able to do (at least without writing or using your own equivalents) is decide what type of objects or how many you want to create at run-time, so you'd be limiting yourslef.

JohnMcG
  • 8,283
  • 5
  • 36
  • 49
-2

[updated]

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

Object o = new Object();

Before creating new instance of class Object, you cannot use it. (Unless you have static methods.)(this is just one example of usage, sometimes other objects will instantiate or destroy objects that you need/don't need)

There are many good answers here but it is difficult to explain everything about new in one reply on SO, and if you do not understand what happens when new is called then it is difficult to know when to use it. This is one of the most important areas in programming so, after reading basic information here you should study it in more detail. Here is one of possible articles where you could start your research:

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

Topics that you will have to learn in order to understand what happens when you call new, so that you then could understand when to call it (there are more probably but this is what i can think of now): - constructors (and destructors) - static classes and methods ...

user218447
  • 629
  • 4
  • 5
  • 3
    If it's too long to explain why do you bother to write here? – Artem Barger Apr 27 '10 at 12:40
  • Hi Keith, thanks for comment, but please comment my answer and not your interpretation of it. If you read my answer carefully you will notice that I never said that you *must* use NEW, and only NEW, to create an instance of a class. Only that NEW is used to create new instance of a class, which is true. It is also true that one cannot use a class before you instantiate it, unless there are some static things (which i also mention), but then I realized that there are so many things to say about this important subject that the best I can do is point to a better source (answer for Artem). – user218447 Apr 27 '10 at 13:30
  • 2
    @zarko-o Actually what you said was "You need new to create new instance of some class", which is incorrect. – meager Apr 27 '10 at 17:43
  • @meagar I would always like to learn new things. Can you please explain what is wrong in that, that might help me increase understanding. – user218447 Apr 27 '10 at 19:33
  • @zarko-o The issue is that you can create object instances without `new`, you don't *need* it which is how you opened your answer. – meager Apr 28 '10 at 01:33
  • @meagar I see what you mean - wording updated. – user218447 Apr 28 '10 at 05:00
  • @zarko-o I think that why your answer was reacted to so negatively, is that Java programmers migrating to C++ often use new for every object, which is not how things should be done in C++. So I think it was an overreaction in the name of removing incorrect information from SO. – KeithB Apr 30 '10 at 15:10