   calling class constructor explicitly without using new operator
  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 errorThanks.   
placement new;  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).   ;  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....;Quote:Original post by aayudhIs 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.   ;  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.;Quote:Original post by SiCraneFrom 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.