-2

New operator is often confusing for me, and using it can lead to memory leaks if I forget to use delete. Every time I have to allocate an array of objects without knowing its length at compile time, I just use std::vector. What is the point of using new?

Also, why would I need to use new operator to allocate space for just one single instance of an object?

Turms
  • 155
  • 1
  • 6
  • 2
    Well... It really depends whether or not you want to allocate it on the stack or the heap. "Why do hammers exist when I can use saws to cut wood?" – byxor Sep 08 '16 at 12:08
  • Std::vector uses the new operator internally to allocate memory. – Paul Rooney Sep 08 '16 at 12:08
  • 3
    Using std::vector is good (do not use `new` for these use cases)! If it gets to polymorphism you might need an allocated pointer, but you might avoid new by using std::unique_ptr (or std::shared_ptr, if you have to). –  Sep 08 '16 at 12:10
  • @JanHudec How does the `new` keyword _not_ result in memory being allocated to the heap? Admittedly I'm not _that_ experienced in C++. I'm more of a C programmer. – byxor Sep 08 '16 at 12:12
  • @Turms One day you might find yourself using [placement `new`](http://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new), but it's completely different kind of `new`. – LogicStuff Sep 08 '16 at 12:13
  • @BrandonIbbotson, `new` *does* result in memory being allocated on the heap. **So does `vector`**. (sans the placement `new`, which the vector actually does end up using, but that are complex technical details normal user does not care about). – Jan Hudec Sep 08 '16 at 12:13
  • 1
    @LogicStuff, damn, braino. Both methods allocate on the heap, of course. – Jan Hudec Sep 08 '16 at 12:14
  • 2
    This question does not deserve down-votes –  Sep 08 '16 at 12:15
  • Glad you cleared that up. :) – byxor Sep 08 '16 at 12:15
  • Who says that you *should use* `new` for arrays, and that you *need to use* it to allocate space? That's just a wrong statement. *Of course* you do not need to, and most of the time it's indeed better style to not use even a single `new`. – Martin Nyolt Sep 08 '16 at 13:08
  • @DieterLücking Thanks. I am still new to the website, so I would like to know from the downvoters how can I improve my question, so that I can do a better job next time! – Turms Sep 08 '16 at 17:21

4 Answers4

4

You are right. Naked new is evil.

It's main purpose is now being the low-level plumbing underneath the high-level constructs. All the vectors and make_uniques and such end up calling new deep in the standard library.

Also, the low level parts predate the high level ones. Before smart pointers, new was the only option in many cases. In fact, std::make_unique was only introduced in C++14, so even C++11 needs to call new even when using unique_ptr for resource clean-up.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
2

For normal use of dynamic arrays in most code, use std::vector. This is clearly the default and prevents many issues.

However, when you know what you are doing and you want some performance gain in special cases, you might want to see whether new or malloc could help you. Preferrably, you make a new specialized container, so people can keep on using it as a normal container. Or even better you could try making an allocator for an existing container. As you probably noticed, this takes some time and probably needs debugging, so unless there's an actual need, stick with the standard containers and for dynamic arrays, this is std::vector.

stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • Just found out (here :http://stackoverflow.com/a/383518/4746978) that there is actually no performance gap between new-ed arrays and std::vectors. – Turms Sep 08 '16 at 15:41
  • 1
    However, there are cases where you want another allocating scheme or you want a similar, but better fitting container for your specific needs. – stefaanv Sep 08 '16 at 15:44
1

Always prefer the standard containers. They have well-defined copying semantics, are exception safe, and release properly.

Objects created by value (i. e. on stack) automatically die when they go out of scope. The destructor call is inserted by the compiler, and the memory is auto-freed upon function return.

Note that C++ is not garbage collected. Therefore, for every new, there must be a corresponding delete. If you fail to put this delete in, then you have a memory leak.

Shravan40
  • 8,922
  • 6
  • 28
  • 48
1

I think your question is related to two things:

  1. The usage of new operator: I just want to make one example. Managing memory with "new" and "delete" gives the programmer control of the program. Some program languages have Garbage Collection, but we don't know when GC will work, which causes uncertainty. However, with systems requiring low latency, this uncertainty will cause trouble.

  2. With "vector", we don't have to create dynamic arrays with new which is good and causes less bugs. Actually it is good to use vector instead of dynamic arrays. But I don't think the usage of vector is conflict with the use of new. It onlye creates a situation you can avoid use new.

  3. Please check the concept of RAII. It's useful for learning C++.

Angela
  • 11
  • 2