Just a simple question. As the title suggests, I've only used the "new" operator to create new instances of a class, so I was wondering what the other method was and how to correctly use it.
4 Answers
You can also have automatic instances of your class, that doesn't use new
, as:
class A{};
//automatic
A a;
//using new
A *pA = new A();
//using malloc and placement-new
A *pA = (A*)malloc(sizeof(A));
pA = new (pA) A();
//using ONLY placement-new
char memory[sizeof(A)];
A *pA = new (memory) A();
The last two are using placement-new which is slightly different from just new. placement-new is used to construct the object by calling the constructor. In the third example, malloc
only allocates the memory, it doesn't call the constructor, that is why placement-new is used to call the constructor to construct the object.
Also note how to delete the memory.
//when pA is created using new
delete pA;
//when pA is allocated memory using malloc, and constructed using placement-new
pA->~A(); //call the destructor first
free(pA); //then free the memory
//when pA constructed using placement-new, and no malloc or new!
pA->~A(); //just call the destructor, that's it!
To know what is placement-new, read these FAQs:
- What is "placement new" and why would I use it? (FAQ at parashift.com)
- placement new (FAQ at stackoverflow.com)
-
2+1: I was wondering what the correct term for *not dynamically allocated* was. *automatic* - Thanks! – Björn Pollex May 04 '11 at 15:16
-
3Also, to appreciate the advantages of using automatic instances, you need to know about [RAII](http://stackoverflow.com/questions/712639/). – Björn Pollex May 04 '11 at 15:20
-
An `auto` variable is also sometimes called a `stack` variable. How many programmers actually use the `auto` keyword anyway? – David R Tribble May 04 '11 at 15:23
-
The third example doesn't always work, because the character array `memory` isn't guaranteed to have the proper alignment for an `A` object. – Derek Ledbetter May 04 '11 at 21:28
-
@Derek: What that has to do with the object? `memory` is not the object. Or how do you think placement-new work? – Nawaz May 05 '11 at 01:38
-
1@Loadmaster: As I commented to a now deleted answer, calling an automatic variable a stack variable can in some cases be incorrect, for instance when such a variable is member of a heap-allocated object. – Björn Pollex May 05 '11 at 06:54
-
@Nawaz: @Derek is correct, a `char[]` array is not guaranteed to be aligned correctly to be converted (placement new'd) into an object. `memory` is not the object, but it will contain the object after it is `new'd`, and therefore must provide the proper alignment for the object. – David R Tribble May 09 '11 at 17:25
-
@Loadmaster: That doesn't make sense to me. Can you explain how one should use placement-new? – Nawaz May 09 '11 at 17:29
-
Your link to parashift.com shows a *forbidden* error message. – Venemo Mar 16 '16 at 10:27
Any of the usual ways: as a local or static variable, or as
a temporary. In general, the only times you use new
in C++ is
when the object has identity and a lifetime which doesn't
correspond to a scope, or when it is polymorphic. (There are
exceptions, of course, but not many.) If the object can be
copied, it's usually preferable to use local instances, copying
those as needed. (Just like you would for int
, in fact.)

- 150,581
- 18
- 184
- 329
Using malloc will give you the same result as new, just without calling the constructor.

- 51
- 2
-
Not completely wrong, so no -1... This is somewhat correct only if constructor does nothing and class does not have any virtual methods... – Alexei Levenkov May 05 '11 at 04:48