3

I'm reading the chapter 13 of "Thinking in c++". The following comes from the book.

MyType *fp = new MyType(1, 2);

at runtime, the equivalent of malloc(sizeof(MyType)) is called, and the constructor for MyType is called with the resulting address as the this pointer, using (1, 2) as the argument list. By the time the pointer is assigned to fp.

I'm confused by the bold sentence. What does it mean?

Community
  • 1
  • 1
Fihop
  • 3,127
  • 9
  • 42
  • 65

2 Answers2

5

It's a very loose explanation, but it's basically saying that the result is a memory location, just like malloc would return, and at that memory location an object is constructed (this is a pointer to the current object) using the constructor with that argument list.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    emphasis on _very loose explanation_, see [this](http://stackoverflow.com/questions/7194127/how-should-i-write-iso-c-standard-conformant-custom-new-and-delete-operators/) for how much `operator new` has to do beyond `malloc` – David Feb 12 '13 at 21:56
  • One more question, when and how the this pointer is initialized? – Fihop Feb 12 '13 at 22:19
  • @FihopZz when the object is created - before the constructor body enters and before member initialization. – Luchian Grigore Feb 12 '13 at 22:26
  • Hi Luchian, I'm kind of confused by your answer. "before the constructor body enters and before member initialization". member initialization must be inside the constructor which means before the constructor body entering indicating before member initialization. Am I right? – Fihop Feb 13 '13 at 15:02
  • @FihopZz no, some members are initialized before the constructor body (think initialization lists, and all non-POD members) – Luchian Grigore Feb 13 '13 at 15:27
4

When the new operator allocates memory dynamically, it returns a pointer to that memory (similar to how malloc() works in C).

In C++, every non-static method has access to the current object it is called on (else C++ programmers around the world would be in serious trouble). This is an "implicit argument" of the methods, in the constructors as well, and one can access it through the keyword this.

What the sentence means is that after creating the object, the operator will call the constructor on the memory it just allocated. Because this is the only thing that makes sense. :)

  • One more question, when and how the this pointer is initialized? – Fihop Feb 12 '13 at 22:20
  • @FihopZz Here and by assigning it to the address of the allocated memory region. –  Feb 12 '13 at 22:20
  • initializing the this pointer happens before calling the constructor or inside the constructor? – Fihop Feb 12 '13 at 22:25
  • @FihopZz Think a bit about that. If it wasn't already initialized when the constructor is called, then in the constructor, `this` would be garbage, and your program would probably segfault each time you dynamically allocate an object. –  Feb 12 '13 at 22:27
  • Hi, H2CO3. I'm kind of new to c++. Maybe the question is stupid. I'm still thinking maybe initializing the this pointer is the first task inside constructor. – Fihop Feb 12 '13 at 22:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24402/discussion-between-fihopzz-and-h2co3) – Fihop Feb 12 '13 at 22:32