6

This is a simple question :

Does using new operator return a pointer of type (void *)? Referring to What is the difference between new/delete and malloc/free? answer - it says new returns a fully typed pointer while malloc void *

But according to 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 it returns a pointer of type (void *), if it returns (void *) I have never seen a code like 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 by calling: operator new (sizeof(MyClass))
      // and then constructs an object at the newly allocated space

  std::cout << "2: ";
  MyClass * p2 = new (std::nothrow) MyClass;
      // allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
      // and then constructs an object at the newly allocated space

So MyClass * p1 = new MyClass calls operator new (sizeof(MyClass)) and since throwing (1)
void* operator new (std::size_t size) throw (std::bad_alloc);
it should return (void *) if I understand the syntax correctly.

Thanks

Community
  • 1
  • 1
Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • 2
    [Shameless plug](http://stackoverflow.com/a/8962536/775806) – n. m. could be an AI May 02 '13 at 18:44
  • 1
    [cppreference entry for new-expression](http://en.cppreference.com/w/cpp/language/new) – dyp May 02 '13 at 18:52
  • @DyP Ok.. got it .. what u meant to say `The new-expression (new int) uses an allocation function (operator new). The allocation function only provides storage, new-expression new type-id returns a pointer to type-id (or throws)`.. Thanks – Gaurav K May 02 '13 at 18:55

3 Answers3

16

You are confusing operator new (which does return void*) and the new operator (which returns a fully-typed pointer).

void* vptr = operator new(10); // allocates 10 bytes
int* iptr = new int(10); // allocate 1 int, and initializes it to 10
David G
  • 94,763
  • 41
  • 167
  • 253
john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    Standard refers to the latter one as *new-expression*. – LihO May 02 '13 at 18:41
  • One way of thinking about it is that the `new` expression "converts" or "casts" memory to objects: void pointer goes in, object pointer comes out. – Kerrek SB May 02 '13 at 18:42
  • john Referring to http://www.cplusplus.com/reference/new/operator%20new/ `std::cout << "1: "; MyClass * p1 = new MyClass; // allocates memory by calling: operator new (sizeof(MyClass)) // and then constructs an object at the newly allocated space` it calls `operator new (sizeof(MyClass))` , so basically it should return `(void *)` as per your argument – Gaurav K May 02 '13 at 18:43
  • 1
    @GauravK The new-expression (`new int`) _uses_ an allocation function (`operator new`). The allocation function only provides storage, new-expression `new type-id` returns a _pointer to type-id_ (or throws). – dyp May 02 '13 at 18:50
  • 1
    @KerrekSB - "converts", not "casts". `` In my usual pedantic mode: a **cast** is something you write in your source code to tell the compiler to do a **conversion**. – Pete Becker May 02 '13 at 19:17
0

void * does not need to be cast to a higher type during assignment only old c code from before the existence of void * requires casting as the old malloc signature returned a char * instead.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
0

new will return a pointer of what ever type you are making an instance of. operator new return pointer to void. new is fairly common while operator new is a little more unique.

Joe Tyman
  • 1,417
  • 5
  • 28
  • 57