Chinmoy
Chinmoy

Reputation: 503

A *a = new A(); Does this create a pointer or an object?

A *a = new A();

Does this create a pointer or an object?

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

Upvotes: 21

Views: 1671

Answers (7)

goodies
goodies

Reputation: 543

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.

Upvotes: 4

Paul
Paul

Reputation: 141829

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`

Upvotes: 44

ZhijieWang
ZhijieWang

Reputation: 453

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.

Upvotes: 0

user3624760
user3624760

Reputation:

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

Upvotes: 3

George Artemiou
George Artemiou

Reputation: 3166

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

Upvotes: 1

NPE
NPE

Reputation: 500327

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.

Upvotes: 15

stardust
stardust

Reputation: 5988

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();

Upvotes: 11

Related Questions