Jump to content
  • Advertisement
Sign in to follow this  
aayudh

calling class constructor explicitly without using new operator

This topic is 3078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Is there a way to call the constructor explicitly without the new operator ? I tried the following but it did not compile.
uint8_t*mem = malloc(size);

objptr = reinterpret_cast<classtype *>(mem);
objptr->classtype(); //this line gives error
Thanks.

Share this post


Link to post
Share on other sites
Advertisement
If you do this you've also got to call the destructor manually, and ensure that the pointer returned by malloc is correctly aligned (if you want to be completely safe on most architectures, align it to a multiple of 16 bytes).

Share this post


Link to post
Share on other sites
From the C standard's description of malloc(), realloc() and calloc():
Quote:
The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated....

Share this post


Link to post
Share on other sites
Quote:
Original post by aayudh
Is there a way to call the constructor explicitly without the new operator ?


You can not call a constructor. You may only call functions and the constructor is not a function. You can construct an object in place using placement new (which is what it appears you are trying to do) but that still uses the new operator.

Share this post


Link to post
Share on other sites
Awesome. Placement new worked.
But..actually what I learnt is all I need to do is overload the placement new style operator for special allocations.

inline void* operator new(unsigned int numbytes, void *ptr) {
return ptr;
}

This did the trick.


Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
From the C standard's description of malloc(), realloc() and calloc():
Quote:
The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated....
In the real world this really only means it will be 8 byte aligned, while some data types or structures may actually require 16 byte alignment (or more). In some cases on some hardware you may even be required to use 128 byte alignment, which malloc isn't going to do despite what the specification says about "any type of object"...
Quote:
Original post by MSVC++
To create an array whose base is properly aligned, use _aligned_malloc, or write your own allocator. Note that normal allocators, such as malloc, C++ operator new, and the Win32 allocators return memory that will most likely not be sufficiently aligned for __declspec(align(#)) structures or arrays of structures.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!