Question

I'm starting to learn C++ and about pointers. However I'm a little confused in their initialization. From what I understand, if I have some class X, the following code is valid:

X* pointer = new X();

This confuses me because I'd expect you'd want to initialize a pointer by giving it the address of an object, as opposed to the object itself, as in:

X* pointer = &(new X());

Does C++ automatically covert the former to the latter? Thanks.

Was it helpful?

Solution

Operator new returns the address of the created object. It does not return the object itself.

Also you could use a reference to the created object. For example

X &x = *( new X() );
//...

delete &x;

OTHER TIPS

new is defined to return a pointer, not an object.

Typically, new allocates memory for an object on the heap, calls the constructor for that object which initializes that memory, then returns a pointer to the address of the memory that was allocated.

The key bit of information regarding your scenario is that despite the object creation side effects of new the return value is a pointer, and not the object itself.

You are using the "new" operator. In C++, this operator return the address of the object and not the object

You can use the & operator, but on object, like this

X objectOne;
X* pointer = &objectOne

No, C++ does not convert the former to latter because these are not the same.

X * ptr = new X();

This statement is calling the constructor of the class X to initialize the pointer. Which is necessary otherwise, you may receive null reference exceptions, i.e. the compiler may say that you are referring to something that hasn't been initialized, hence initialization is important.

Then once the pointer is initialized, you can make it point to an object like:

X object = <some value>;

X* ptr = &object; 

Welcome to C++ and happy coding :)

Let's say you have a struct with two integers that are initialized to 1 and 2 in the struct constructor:

struct two_ints
{
    int a;
    int b;

    two_ints() : a(1), b(2) {}
};

Let's also say you have a 45-byte heap memory space, like the graph shown below.

        1       2       3       4
0   5   0   5   0   5   0   5   0   5
|||||||||||||||||||||||||||||||||||||

Now, if you instantiate a two_ints struct on the 45-byte heap like this:

two_ints* ints = new two_ints();

Then, graphically, what operator new will do for you in this example is this:

        1       2       3       4
0   5   0   5   0   5   0   5   0   5
|||||||||||||||||||||||||||||||||||||
                aaaabbbb
                ----
                ^   ----
                |   ^
                |   |
                |   +-- 4 bytes for field b
                |
                +-- 4 bytes for field a
                |
                +-- on address 20 in this 45-byte memory space

Or in words, it will:

  • Allocate 8 bytes of memory for 2 4-byte integers, starting at memory address 20 (in this example).
  • Run the constructor that assigns the values 1 and 2 to a and b, meaning that the aaaa and bbbb memory slots in the diagram above is set to 0001 and 0002, or 1000 and 2000, depending on the platform endianess.
  • Return the address 20.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top