Take the 2-minute tour ×
A *a = new A();

Does this create a pointer or an object?

I am a c++ beginner, so I want to understand this difference.

share|improve this question
If it only created a pointer, what would that pointer point to exactly? –  Mat Apr 20 '13 at 6:17
When you purchase a new home, have you obtained a new street address, or a new house? –  Mehrdad Apr 20 '13 at 6:18
A pointer is also an object itself. –  Nawaz Apr 20 '13 at 13:07
A *a is the pointer. It can hold an address. new A() returns the address of a newly-created object. –  Hot Licks Apr 20 '13 at 13:19

7 Answers 7

Both: you created a new instance of A (an object), and created a pointer to it called a.

You could separate it into two statements:

A *a;        // Declare `a` of type: pointer to `A`

a = new A(); // create a new instance of `A` and
             // assign the resulting pointer to `a`
share|improve this answer

This creates an object of type A on the heap, and stores a pointer to it in a (that is stored on the stack).

P.S. Don't forget to call delete a when you're done with it, so that A can be destroyed and its memory given back to the heap. Better still, turn a into a smart pointer.

share|improve this answer
I hope you do not mind the tiny edit to clarify where a is stored. –  Ed Heal Apr 20 '13 at 6:21
what if constructor of A class isn`t exception save - then exception can be thrown and no instance will be created, also after you call delete in such situation you will get a run-time error. –  spin_eight Apr 20 '13 at 6:22
More like don't forget to use a smart pointer instead of worrying about safely deleting it. –  chris Apr 20 '13 at 6:28
@spin_eight - initialize the pointer to 0; then if the constructor for A throws an exception, delete a is still ok. No need for smart pointers or for checking anything. –  Pete Becker Apr 20 '13 at 11:59
@ Pete Becker actually I mean that this answer could contain more detailed info, that it doesnt cover all possible variants, and to cover them all a lot of info should be given. I think that it will be good to provide to the topic starter refernces on books/sites for begginers when trying to cover such theam in a rather short answer. Also to add - operator new can be overloaded, so in that case you cant garantee that an instance of the class will be created –  spin_eight Apr 21 '13 at 6:39

It creates both.
It creates object of type A* on the stack and object of type A on the heap.

Equivalent to

A *a;
a = new A();
share|improve this answer

It will create both object as well as pointer.

Firstlly new A() is called and if you have default constructor then it call that constructor and initialize that object with default values otherwise it will initialize with defaul values.Because we are using new keywork then it will allocate the memory for object A on heap.New keyword is used to allocate memory dynamically on heap.NEW returns the starting address of the object.

After that A type pointer which will have the address of object A() returned by the new operator.

share|improve this answer

As others have said, it creates both. But the object A is created on free store and now you have to remember to manually delete it. Remember, that any function in C++ can throw an exception unless it is declared as noexcept. So now not only you have to remember to delete when no exception is thrown, you will now also need to imagine all the paths the code could take and write appropriate try-catch blocks to manually delete your object.

What you are doing is called naked new and it is an easy way to shoot yourself in the foot.

Thankfully, C++11 has a solution to this problem: smart pointers. Consider the semantics of your pointer, will it be owned by a one entity or shared between several ones? In the former case, you'll need std::unique_ptr:

#include <memory>

std::unique_ptr<A> a(new A{});

Now you don't need to call delete, memory management has been taken care of for you. In the latter case, you'll need std::shared_ptr

#include <memory>

std::shared_ptr<A> a(new A{});

But the definition on C++ sequence points may not guarantee that this way of creating smart pointers will always be safe, without getting into details:

std::shared_ptr<A>(new A{new B, new C});

May produce memory leaks, to avoid this, use std::make_shared

#include <memory>

auto a = std::make_shared<A>(); // a now holds a shared_ptr to A

Unfortunately, when finalizing C++11, committee forgot about std::make_unique. This should be fixed in C++14:

#include <memory>

auto a = std::make_unique<A>(); // a now holds a unique_ptr to A
share|improve this answer

It creates both.

First you create an object of type A and then store a pointer to it

For more information on pointers please have a look at here

share|improve this answer

an object and pointer. The object has to have a pointer for you to reference, however, you can have one one object pointed by several pointer.

share|improve this answer

Your Answer


