Does new return (void *) in C++? -


this simple question :

does using new operator return pointer of type (void *)? referring what difference between new/delete , malloc/free? answer - says new returns typed pointer while malloc void *

but according http://www.cplusplus.com/reference/new/operator%20new/

throwing (1)     void* operator new (std::size_t size) throw (std::bad_alloc); nothrow (2)  void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw(); placement (3)    void* operator new (std::size_t size, void* ptr) throw(); 

which means returns pointer of type (void *), if returns (void *) have never seen code myclass *ptr = (myclass *)new myclass;

i have got confused .

edit

as per http://www.cplusplus.com/reference/new/operator%20new/ example

std::cout << "1: ";   myclass * p1 = new myclass;       // allocates memory calling: operator new (sizeof(myclass))       // , constructs object @ newly allocated space    std::cout << "2: ";   myclass * p2 = new (std::nothrow) myclass;       // allocates memory calling: operator new (sizeof(myclass),std::nothrow)       // , constructs object @ newly allocated space 

so myclass * p1 = new myclass calls operator new (sizeof(myclass)) , since throwing (1)
void* operator new (std::size_t size) throw (std::bad_alloc);
should return (void *) if understand syntax correctly.

thanks

you confusing operator new (which return void*) , new operator (which returns fully-typed pointer).

void* vptr = operator new(10); // allocates 10 bytes int* iptr = new int(10); // allocate 1 int, , initializes 10 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -