10

I know about new keyword that returns pointer so return type is void*. My stupid question is that why new must return void* instead of void& ? Mean when object created by new they could return address of that object using address of & operator.

I know difference between pointer and reference. But at the end we work with pointer instead of reference. Please clear my confusion thanks.

  • If `new` returned a `void *`, then `int *p = new int;` wouldn't compile. This assumes you mean the language `new` and not the stuff in ``. – chris Jul 05 '15 at 03:29
  • 2
    Because references are not sugary pointers, they are aliases. – Captain Obvlious Jul 05 '15 at 03:31
  • Please read about the difference between operator new and new operator here http://stackoverflow.com/questions/16345340/does-new-return-void-in-c – demonplus Jul 05 '15 at 03:33
  • @demonplus I already read all differences. But we use left pointer and then we can do anything because we are working with pointer not reference. I mean at the end we works with pointer instead of reference. –  Jul 05 '15 at 03:38
  • 9
    Not an answer since I don't have any good evidence, but my understanding for why it is a pointer and not a reference is: 1) C++ didn't have exceptions when it was first created. So if you failed to allocate memory, the way you indicated failure was to return null. You cannot do that with references. 2) C++ incidentally also didn't have references when it was first created, so it wasn't a possibility anyway. 3) Even with exceptions and references, there are still overloads of `new` that are explicitly exception-less, and null is used to indicate failure in these cases. – GManNickG Jul 05 '15 at 03:42
  • @GManNickG: Although I also don't have evidence, I am quite sure that every point you made is correct, and would upvote it as an answer without asking for references. – Benjamin Lindley Jul 05 '15 at 03:45
  • @GManNickG to close..!! But waiting for extra knowledge. –  Jul 05 '15 at 03:47
  • Pretty sure what GManNickG said sums up all there is to it. Also, you can always use `*new` if you really want to use reference instead of pointer. `Object &obj = *new Object()` is valid. – Havenard Jul 05 '15 at 03:50
  • @Havenard i know about that, I'm not discussing, how to use reference instead of pointer. My question is that why `new` returns a pointer. –  Jul 05 '15 at 03:53
  • 2
    For backward compatibility. They are always improving C++, but not changing it. Old code is supposed to work. Besides, you can `delete` what you create with `new`, but then you would have a reference to an unexisting object, which is illegal. – Havenard Jul 05 '15 at 03:54
  • @Havenard I really liked your this point. –  Jul 05 '15 at 03:57
  • Is there some compelling reason why it should or could return a reference? Reference to what? And how would `delete` work? This suggestion, if that's what it is, would cause more problems than it would solve. – user207421 Jul 05 '15 at 03:57
  • @Havenard but what you say about your own comment? `Object &obj = *new Object()` how delete will work here? –  Jul 05 '15 at 04:04
  • Not sure, perhaps `delete &obj`, but even though this is expected to work, this is aswell undefined behaviour. – Havenard Jul 05 '15 at 04:18
  • @Havenard so nothing to say about my question? or we just can say returning a pointer is useful and safer than a reference? –  Jul 05 '15 at 04:21
  • Its more flexible, its more coherent since you are supposed to be able to `delete` it later, and its backward compatible. – Havenard Jul 05 '15 at 04:34
  • Dynamic memory allocation is all about pointers. It would be stupid to force the user to always take the address of the returned object. – The Paramagnetic Croissant Jul 05 '15 at 11:43

2 Answers2

8

New returns a pointer of whatever type you used to allocate space in memory, not a void*

 auto p1 = new int[5];   // returns a pointer to an int pointing to the 1st element
 auto p2 = new short[5]; // returns a pointer to a short pointing to the 1st element

References can only be assigned once, naked pointer can be assigned several times. You can delete the memory and allocate more and assign it to the same pointer. That's the main difference.

delete[] p1;
p1 = new int[20]; 
  • 1
    what's about this one.. `ClassName *obj=&obj2;` Suppose obj2 created with `new` keyword and assigned to `*obj` so. Why new didn't return `&obj2` ? there could be a problem with this? – Asif Mushtaq Dec 16 '15 at 14:58
4

I believe that new returns a pointer since it's more flexible than a reference, and I think it has much to do with backward compatibility to older C++ and C code where NULL was used to indicated allocation failure. Whereas setting a non const reference to null is invalid.

Also new offers no way to name the objects that it allocates, instead it returns a pointer to the object it allocates. You can't bind a non const lvalue reference to an unnamed object.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62