References or Pointers. Which syntax do you prefer?

Started by
24 comments, last by SeraphLance 9 years, 12 months ago

Ive always preferred pointers over references. By using pointers when I look at the code its clear which variable holds a memory adress and which is an actual object. ...you look at a pointer, you now that its one. Now the advantages of references are becoming more important to me than this.

So... if you only consider the syntax, which do you prefer? References or pointers.

Advertisement
It's not really a case of preference. Some things require references, some require pointers.

As out params to methods, I work with libraries that use pointers as it is clear at the point of call that a param is an out param which is reasonable but unnecessary in modern IDEs.

Where I have a choice, I prefer a reference unless the value can be maybe null.

I tend to prefer by reference when possible, but pointers are advantageous when you can have a null value. References can "force" the existence of a value (there are exceptions to this).

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

It's not just personal taste. Reference & Pointers tell you about the code.

  • const Foo &myFoo means this is an input variable, which cannot be null. Probably it's a large struct and we're using reference to avoid passing by value. If the reference is actually null, something really bad happened before entering the function. GDB & MSVC both support showing the address (just print &myFoo)
  • const Foo *myFoo means this is an input variable, but the pointer may be null. i.e. optional. Check the documentation to see if you can assume it cannot.
  • Foo &myFoo. This is an output variable. You're expected to modify it. Could also be input, but this is discouraged for many reasons.
  • Foo *myFoo. This could be an output variable. Or could be that you need to write to raw memory (i.e. gpu pointer) and/or do some pointer addressing math. Could be null (you may not be always expected to modify it). Could also be input. The most ambiguous of all.

Additionally, pointers can have a few more qualifiers that sadly references do not, such as __restrict, which is very powerful in code optimization.

Pointers are optional or are expected to change during their lifetimes, references are non-optional and won't change.

If during the lifetime of your pointer, it isn't expected to change and it's not optional - it should probably be a reference.

Where I have a choice, I prefer a reference unless the value can be maybe null.


I've taken a more "modern" approach here and prefer always using reference with a concept of Optional types if I need something nullable. Similar to the proposed C++ std::optional.

The primary difference between a reference and a pointer is that a pointer can be rebound (you can point it at something else) while a reference cannot. A reference is an alias to an object instance while a pointer is an entirely different concept (it is its own value representing a memory address). As you (almost) never actually need rebinding, you likewise almost never need pointers and can just use references most of the time. Optional types let you return a nullary value when appropriate, be it a reference (or some other non-copyable/non-moveable handle), a primitive like an int, or so on, with a unified interface for nullary checking/handling.

// a short alias
template <typename T>
using opt = std::experimental::optional<T>;

// function using optional to return either a reference or 'null'
Bar g_Bar;
opt<Bar&> foo() { return condition ? g_Bar : nullopt; }

auto bar = foo();
if (bar)
  use_bar(*bar);
It'd be nice if the syntax were a little cleaner in C++, but then it'd also be nice if owning pointers (std::unique_ptr) were a built-in feature like in Rust. C++ is not for people afraid of typing or syntax spew. tongue.png

Sean Middleditch – Game Systems Engineer – Join my team!

Thanks for the answers so far!


A reference is an alias to an object instance while a pointer is an entirely different concept (it is its own value representing a memory address)

Thats the thing... that they are a different concept and use different syntax. (But generally they are handled the same way in the machine code.) In the case of the pointer its obvious that you dont operate on a "real object" but with a reference it isnt.

A common style that I've seen is const-references for in-params (which should act like pass-by-value, but are expensive to copy) and pointers for all out params.
The rationale behind this is that it makes out-params very obvious at the call-site, similarly where in other languages the caller must use an 'out' keyword.

It'd be nice if the syntax were a little cleaner in C++, but then it'd also be nice if owning pointers (std::unique_ptr) were a built-in feature like in Rust. C++ is not for people afraid of typing or syntax spew. :P

That would completely kill C++ as being a pay-for-what-you-use / opt-in language. Most embedded systems that I've worked on still use raw-pointers, or a smart pointer that acts like a raw one, but only performs leak detection during development.

That would completely kill C++ as being a pay-for-what-you-use / opt-in language. Most embedded systems that I've worked on still use raw-pointers, or a smart pointer that acts like a raw one, but only performs leak detection during development.


Owned pointers are a language semantic that have _zero_ overhead compared to manually using raw pointers and new/delete in the requisite places. Raw pointers would of course stay around for cases you don't want owned semantics. All an owned pointer does is call delete (or with std::unique_ptr any deleter policy function specified in the template) when the pointer goes out of scope. std::unique_ptr takes up the same space (assuming your deleter policy doesn't require extra space), has no additional runtime overhead (assuming optimizations are on and all the template goo gets inlined and removed), and imposes no additional memory allocation requirements beyond whatever your allocator uses. You can use unique_ptr with pooled objects trivially by allocating from the pool and using a release_to_pool deleter policy instead of the default policy. Reference-counted pointers, GC pointers, and other shared-ownership pointers are a different thing.

Sean Middleditch – Game Systems Engineer – Join my team!

Yeah true, as long as you introduced new syntax, like C++CLR did with it's "Foo^ foo" type pointers.
I was thinking of the consequences of making &/* do any more than they do currently.

This topic is closed to new replies.

Advertisement