0

Is there any significant difference between the two statements below?

MyClass &ref = *(new MyClass);

MyClass *ptr = (new MyClass);
  • 1
    Pointer can be changed. Reference can't. – Ron Sep 11 '18 at 11:49
  • 2
    BTW: the outer parentheses are useless. – Jabberwocky Sep 11 '18 at 11:50
  • 2
    why would you write any of the two? Prefer `MyClass obj;` – 463035818_is_not_an_ai Sep 11 '18 at 11:50
  • 1
    While `MyClass &ref = (*(new MyClass));` is technically correct C++ code, it goes against most conventions and leads to "WTF" code full of errors, so please, don't use that. – Dan M. Sep 11 '18 at 11:52
  • 1
    If you had written `new (std::nothrow) MyClass` in the first version then bad things could happen. For me then, that makes the second one the superior choice. – Bathsheba Sep 11 '18 at 11:54
  • 2
    One significant difference is that you learn pretty quickly not to use the first. Learning to not use the second usually takes longer. – molbdnilo Sep 11 '18 at 11:56
  • The first is surprising, the second is not. Try to never surprise anybody in your source. And strongly seconding previous comments insofar as you should, **if** you need to allocate memory yourself, best be using smart pointers, not "naked" ones. – DevSolar Sep 11 '18 at 12:40

3 Answers3

4

First is a reference, second is a pointer. Reference cannot be changed.

Overall avoid handrolled memory management (this means not writing new/delete at all)

darune
  • 10,480
  • 2
  • 24
  • 62
2

While pointers can be changed, and references cannot, I would still advise against manually manipulating pointers.

A destructor/constructor based memory management can solve so much headache down the line.

I would also consider using smart pointers.

What is a smart pointer and when should I use one?

Loop
  • 233
  • 1
  • 3
  • 13
0

Speaking technically, not hugely.

I mean, you can't rebind ref to some other thing later, in the same way that you can make ptr point to something else (though that's arguably a good thing).

The big issue is in expressing intent. We're all conditioned to see a pointer and think "aha, this may be dynamically allocated, and if nothing else I need to be aware of this object's lifetime and ownership semantics to see whether it must be deleted" (and, if you don't, it's leaked). This is practically never the case with a reference, which we generally take to mean either of:

  • The referent is not dynamically allocated, or
  • The scope that passed me this reference does not intend for me to do anything relating to the referent's lifetime

You're just supposed to use the value and leave it at that.

So, the biggest problem is in clarity of code via conventional coding styles.

But if you were some sort of practical joker you could write the following and it would be perfectly "valid":

int main()
{
   int& ref = *(new int);
   delete &ref;
}

Just remember to use the address-of operator, to get a pointer again for delete!

  Expression    Type    Indirection
                            ∧
    &&expr       T****      |
     &expr       T***       |
      expr       T**        |
     *expr       T*         |
    **expr       T          |
                            ∨
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055