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)
should return
void* operator new (std::size_t size) throw (std::bad_alloc);(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
Post a Comment