6

Possible Duplicate:
In C++, why should new be used as little as possible?

Is it really a bad idea to use 'new' in instantiating a class in C++? Found here.

I get that using raw pointers is ill-advised, but why have a 'new' keyword at all when it's such bad practice? Or is it?

Community
  • 1
  • 1
Stephen Collins
  • 3,124
  • 8
  • 35
  • 57
  • Usually it's unnecessary to `new` types with value semantics. That's what Jerry referred to I suppose. – sbi Oct 01 '11 at 13:24
  • Vote to close. This should be a comment thread on the linked question, as this question is based on a fundamental misinterpreation of that answer! Generally, `new` is not _such bad practice_, and it's not _a bad idea_. The answer you linked recommends removing the `new()`s to address a compile-time error: You can't pass a pointer to a function that doesn't expect one! – Roddy Oct 01 '11 at 13:25
  • If you did away with "raw pointer" in C++ you wouldn't have much left. – Hot Licks Oct 01 '11 at 13:27
  • The past 15 years of C++ library development have been dedicated to finding ways to avoid having to use *new*. Explicit memory management is hard to get right. – Hans Passant Oct 01 '11 at 13:52
  • @HansPassant: I think more important that the difficulties of writing correct manual allocations is the fact the fundamental distinction between automatic and manual *lifetime*, and the fact that the vast majority of all program logic is much better served by automatic objects than by manual ones (thanks in large parts due to clever library building blocks). Voting to reopen, by the way, since the linked question seems to be a bit different (and the answers focus too heavily on the implementation details rather than the much more relevant language semantics). – Kerrek SB Oct 01 '11 at 21:42
  • @Kerrek - Well, no argument from me. Garbage collectors are pretty popular these days, just another aspect of leaving memory management to a library. The C++ angle is that you can break the library dependency *when you have to*. Even though everybody tells you not to. Which is odd advice from the [c++] tag frequenters. Almost sounds like they never used a GC before. Let's leave it closed, way too subjective. – Hans Passant Oct 01 '11 at 21:50

7 Answers7

17

The point is that new, much like a pregnancy, creates a resource that is managed manually (i.e. by you), and as such it comes with responsibility.

C++ is a language for library writing, and any time you see a responsibility, the "C++" approach is to write a library element that handles this, and only this, responsibility. For dynamic memory allocation, those library components already exist and are summarily referred to as "smart pointers"; you'll want to look at std::unique_ptr and std::shared_ptr (or their TR1 or Boost equivalents).

While writing those single-responsibility building blocks, you will indeed need to say new and delete. But you do that once, and you think about it carefully and make sure you provide the correct copy, assignment and destruction semantics. (From the point of exception safety, single responsibility is crucial, since dealing with more than one single resource at a time is horribly unscalable.)

Once you have everything factored into suitable building blocks, you compose those blocks into bigger and bigger systems of code, but at that point you don't need to exercise any manual responsibility any more, since the building blocks already do this for you.

Since the standard library offers resource managing classes for the vast majority of use cases (dynamic arrays, smart pointers, file handles, strings), the point is that a well-factored and crafted C++ project should have very little need for any sort of manual resource management, which includes the use of new. All your handler objects are either automatic (scoped), or members of other classes whose instances are in turn scoped or managed by someone.

With this in mind, the only time you should be saying new is when you create a new resource-managing object; although even then that's not always necessary:

std::unique_ptr<Foo> p1(new Foo(1, 'a', -2.5));   // unique pointer
std::shared_ptr<Foo> p2(new Foo(1, 'a', -2.5));   // shared pointer
auto p3 = std::make_shared<Foo>(1, 'a', -2.5);    // equivalent to p2, but better

Update: I think I may have addressed only half the OP's concerns. Many people coming from other languages seem to be under the impression that any object must be instantiated with a new-type expression. This is in itself a very unhelpful mindset when approaching C++:

The crucial distinction in C++ is that of object lifetime, or "storage class". This can be one of: automatic (scoped), static (permanent), or dynamic (manual). Global variables have static lifetime. The vast majority of all variables (which are declared as Foo x; inside a local scope) have automatic lifetime. It is only for dynamic storage that we use a new expression. The most important thing to realize when coming to C++ from another OO language is that most objects only ever need to have automatic lifetime, and thus there is never anything to worry about.

So the first realization should be that "C++ rarely needs dynamic storage". I feel that this may have been part of the OP's question. The question may have been better phrased as "is it a really bad idea to allocate objects dynamically?". It is only after you decide that you really need dynamic storage that we get to the discussion proper of whether you should be saying new and delete a lot, or if there are preferable alternatives, which is the point of my original answer.

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • 7
    +1 for comparing `new` with unprotected sex, which helps explain why the C++ chat room tends to discuss that often. – R. Martinho Fernandes Oct 01 '11 at 13:56
  • +1 for the first paragraph. I didn't read the rest. Was it a good post? – LarsTech Oct 01 '11 at 23:10
  • This seems to be a very well thought out and complete explanation. Thank you for the clarification – Stephen Collins Oct 02 '11 at 08:52
  • 1
    "The most important thing to realize when coming to C++ from another OO language is that most objects only ever need to have automatic lifetime, and thus there is never anything to worry about." I agree with "most". If you are writing a library or engine - I would imagine a good use case for longer object life. One may argue that you could just return a copy of the object - but I really think that's a waste. – Natalie Adams Sep 16 '13 at 04:42
  • "_C++ is a language for library writing_" is a bit odd to me. It might be *great for* that, but that doesn't mean it is *for* that. Certainly, the origin story of C++ does not read to me as having that purpose in mind, and I've not heard this specific statement before. Can you elaborate on what you meant by it? – underscore_d Oct 27 '18 at 17:28
3

Avoiding new as much as possible, means many benefits, such as:

  • First and foremost, you also avoid delete statements.Though smart pointers can help you here. So this is not that strong point.

  • You avoid Rule of Three (in C++03), or Rule of Five (in C++11). If you use new when designing a class, that is, when your class manages raw memory internally, you probably have to consider this rule.

  • It's easy to implement exception-safe code when you don't use new. Otherwise, you've to face hell lot of problems, making your code exception-safe.

Using new unnecessarily means you're inviting problems. I've seen when an inexperienced programmer uses new, he has often better alternatives, such as usage standard containers, and algorithms. Use of standard containers avoids most problems which comes with explicit use of new.

Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • 3
    The Rule of Three/Five you have to consider when _designing_ a class, not when deciding how to make use of it. The other two are correct, though. – sbi Oct 01 '11 at 13:26
  • @sbi: I know that, that is why I said *"...you **probably** have to consider this rule."*. – Nawaz Oct 01 '11 at 13:29
  • @sbi: I made it clear in my answer, now. – Nawaz Oct 01 '11 at 13:30
  • I think now I get it: Do you mean when you don't `new` class data members?? – sbi Oct 01 '11 at 13:31
  • @sbi: Honestly I didn't understand your question (which part of my answer it refers to?). – Nawaz Oct 01 '11 at 13:33
  • 1
    The one about TRoT/F. When I design a class, I need to decide about implementing copying/moving, no matter whether objects of the class will be created dynamically or automatically. If you are not referring to the usage of the class designed, but to the data members in it being created dynamically or not, then you are right. It's just that this isn't clear from reading your answer. – sbi Oct 01 '11 at 13:38
  • @sbi: Actually, if you've noticed, I've edited my answer, making it clear that I'm talking about designing a class itself, which internally managers memory. – Nawaz Oct 01 '11 at 13:41
2

It's not bad, but everything you allocate with new, has to be deallocated with a delete. This is not always trivial to do, especially when you take into account exceptions. I think that is what that post meant.

Andrea Bergia
  • 5,259
  • 1
  • 22
  • 38
  • 4
    _everything you allocate with `new`, has to be deallocated with a `delete`_ - well, yes, but smart pointers will do that for you automagically in the majority of cases. – Roddy Oct 01 '11 at 13:17
0

It's not a "bad idea to use new" -- the poster misstated his case rather badly. Rather, using new and not give you two different things.

new gives you a new, separately allocated instance of the class, and returns a pointer to that instance.

Using the class name without new creates an automatic instance of the class that will go "poof" when it passes out of scope. This "returns" the instance itself, not a pointer (and hence the syntax error in that other thread).

If you use new in the referenced case and added * to pass the compiler, it would result in a leaked object. If, on the other hand, you were passing a parameter to a method that was going to store it somewhere, and you passed a non-newed instance, making it work with &, you'd end up with a dangling pointer stored.

Hot Licks
  • 44,830
  • 15
  • 88
  • 146
  • 3
    Actually I don't think Jerry misstated, but I think wtfsven understood poorly. That was an answer given to a specific question, and that answer was right, IMNSHO. And I agree that, in general, you should avoid `new`/`delete` when possible. – sbi Oct 01 '11 at 13:23
  • I meant that he in no way explained WHY it was so -- he basically just said "don't use new if you can avoid it", which didn't even begin to explain the differences. – Hot Licks Oct 01 '11 at 13:25
  • 1
    @Daniel But explaining that universally is rather non-trivial and didn’t really belong in the other answer. It belongs here. ;-) – Konrad Rudolph Oct 01 '11 at 13:49
  • So why don't you post an explanation? ;) – Hot Licks Oct 01 '11 at 14:11
0

new what you delete, and free what you malloc (don't mix them, you'll get into trouble). Sometimes you have to use new, as data allocated with new will not fall out of scope... unless the data pointer is lost, which is the entire issue with new. But that is err on the side of the programmer, not the keyword.

GeirGrusom
  • 1,000
  • 5
  • 18
  • 2
    Don't use malloc in C++. – Cat Plus Plus Oct 01 '11 at 13:22
  • @CatPlusPlus: ... unless you want to or need to :-) (Looking at _you_, [malloc-allocator](http://www.aoc.nrao.edu/php/tjuerges/ALMA/STL/html-3.4.6/malloc__allocator_8h-source.html).) – Kerrek SB Oct 01 '11 at 13:32
  • "_Sometimes you have to use new, as data allocated with new will not fall out of scope..._" There are usually better ways to solve this problem, like using an `std::shared_ptr` or another resource-managing, ownership-sharing class, which still handle all the ugly details like `new`/`delete` and avoid the user having to get them all right (and statistically, inevitably forgetting to do so). – underscore_d Oct 27 '18 at 17:33
0

It depends on what the code needs. It the reply you refer to, the vector contains client instances, not pointers to client instances.

In C++, you can create object directly on the stack, without using new, like V1 and V2 in the code below:

void someFct()
{
     std::vector<client> V1;
     //....
     std::vector<client*> V2;
}

When using V2, you will have to create new client instance with the new operation, but the client objects will not be released (deleted) when V2 will go out of scope. There is no garbage collector. You have to delete the objects before leaving the function.

To have the created instances deleted automatically, you can use std::shared_ptr. That make the code a bit longer to write, but it is simpler to maintain in the long term:

void someFct()
{
    typedef std::shared_ptr<client> client_ptr;
    typedef std::vector<client_ptr> client_array;
    client_array V2;

    V2.push_back(client_ptr(new client()));

    // The client instance are now automatically released when the function ends, 
    // even if an exception is thrown.
}
PRouleau
  • 514
  • 3
  • 11
  • What is the point of showing pointer-based approaches, complaining that they won't destroy the objects at the end of the function, and then adding that back by using `shared_ptr`? Why wouldn't you just store the objects by value as in the original `V1` case? There's no point putting them through dynamic allocation if you don't need them to outlive the current scope, and this doesn't. – underscore_d Oct 27 '18 at 17:35
0

Is it really a bad idea to use 'new' in instantiating a class in C++?

It’s often bad because it’s not necessary, and code gets much easier when you’re not using it spuriously. In cases where you can get away without using it, do so. I’ve written whole libraries without once using new.

I get that using raw pointers is ill-advised, but why have a 'new' keyword at all when it's such bad practice? Or is it?

It’s not universally bad, just unnecessary most of the time. But there are also times when it’s appropriate and that’s why there is such a keyword. That said, C++ could have gotten away without the keyword, since new conflates two concepts: 1. it allocates memory, and 2. it initialises the memory to an object.

You can decouple these processes by using other means of memory allocation, followed by a constructor invocation (“placement new”). This is actually done all over the place, such as the standard library, via allocators.

On the other hand, it’s rarely (read: never) meaningful for client code to manage uninitialised memory so it makes sense not to decouple these two processes. Hence the existence of new.

Konrad Rudolph
  • 482,603
  • 120
  • 884
  • 1,141