开发者

How does it work, Test *pObj = new Test(); as constructor does not return anything

开发者 https://www.devze.com 2023-01-13 09:50 出处:网络
I am trying to get better at c++. I have a Test class and below code in main(). Test *pObj = new Test();

I am trying to get better at c++. I have a Test class and below code in main().

Test *pObj = new Test();

If we debug by steping one by one instruction, First it goes to new function to allocate memory, then it calls constructor. Then it comes back to main() function. As we all know, constr开发者_如何转开发uctor does not return anything. In that case, how come pObj will have the pointer which is allocated by new ? Any idea how it is implemented by compiler ?


When you use a new expression the compiler generates code to allocate memory and then call the constructor on the allocated memory to create a new object. If successful, it returns a pointer to the new object.

Constructors have no return values, the compiler just adds a call to the constructor on a piece of memory where it needs the new object to be constructed. It's not necessary for the constructor to return the location of the object, the code already knews where the object must be; it (effectively) passed it to the constructor.


Yes.. The constructor didn't return anything.. But the new operator constructs and returns the allocated memory address of the instance of Test class which is being pointed by pObj..


I think, i got the answer. new operator allocates memory and makes a call to constructor. It will be passed this pointer as the same pointer that new allocated. Once constructor returns, control comes back to new operator implementation of compiler.(Not the main() function). Then, once new returns, as it gives the allocated pointer, pObj will have that pointer.


new and delete are operators. By default the new operator allocates the memory for the object, and returns the pointer to that memory. A constructor is then called if one exists for the object being created, based on the syntax of what comes after the new operator. The object is already allocated before the constructor is called, so the constructor doesn't need to return anything. It is essentially a void method that is called after allocation.

The global new operator is often implemented as a function, such as void* new(size_t s), where s is the size of the object to be allocated. The compiler figures the size of the object to be created under the hood and passes it to new, returns the pointer to the allocated memory, and then calls the constructor.

You can override the new operator either globally or an a per-class basis to add your own behavior.


The return value you should care about is the one 'returned' by new, in that case the address of the newly allocated object.

new can be implemented by a call to malloc for example, which truly return the address (edit: see comments bellow).


$5.3.4/1 - "If the entity is a non-array object, the new-expression returns a pointer to the object created. If it is an array, the new-expression returns a pointer to the initial element of the array."

Note the new expression and operator new are not one and same thing.

$5.3.4/8- "If the allocated type is a non-array type, the allocation function’s name is operator new and the deallocation function’s name is operator delete. If the allocated type is an array type, the allocation function’s name is operator new[] and the deallocation function’s name is operator delete[]."

0

精彩评论

暂无评论...
验证码 换一张
取 消