Foognostic blogs Seeking knowledge of foo

17Dec/08Off

C++ versus Java, or references to references versus values of references.

A few years of Java really make it hard to write C++ again. Quick, which language is this?

    stuff.yodel();

That works in Java and C++ but for different reasons. It's usually safer in C++.

In Java that code is less safe because it hasn't been tested for null. That is, one of the following already happened:

  1. Stuff stuff = null;
  2. Stuff stuff = new Stuff();
  3. Stuff stuff = Stuff.class.newInstance();
The guarantee is that each reference will have an intentional value, which is more than C++ promises for pointer values.

C++ gives you stronger references. For that same line of code, one of these already happened:

  1. Stuff stuff;
  2. Stuff *stuff_p = new Stuff(), &stuff = *stuff_p;
  3. Stuff& f2() { Stuff stuff; return stuff; }
Which means...
  1. This
    stuff
    refers to an object allocated on the stack. It's there, baby -- as much as any
    int
    or
    float
    .
  2. This
    stuff
    refers to an object successfully allocated in the heap.
  3. Very bad.
    f2
    destructed the stuff before giving it to you. Just keep compiler warnings on and it will bark "warning: reference to local variable 'stuff' returned"

What wasn't shown for C++ was

stuff->yodel()
. That followed one of:
  1. Stuff *stuff;
  2. Stuff *stuff = null;
  3. Stuff *stuff = new Stuff();
  4. std::auto_ptr<Stuff> stuff(new Stuff());
Which means...
  1. Uninitialized pointer, a.k.a. Runtime Russian Roulette. Unfortunately I couldn't get g++ to whine about this even with
    -Wall -Wpedantic -Wextra
    .
  2. Null pointer, a.k.a. the minimum guarantee of Java.
  3. Hmm, I don't see a delete any where around here... uh-oh...
  4. There you go. You've got a smart pointer on the stack to prevent memory leaks, while preserving the expected
    ->
    syntax.

So WHAT? Really now!

Java guarantees that a reference will be initialized, and that it must always be tested for null. C++ warms the hearts of bearded old curmudgeons with more types of references. Each type is useful alone and in sometimes in conjunction (e.g. non-const references to pointers for output parameters). This is useful and important information to know. Draping Object everywhere and automating garbage collection is a lowbrow kludge.

Footnote 1: Groovy's safe navigation operator helps test for null by GOTOing the next safe expression. An interesting idea, and hopefully self-preventative. If I saw code with lots of '?' it would be up for a rewrite sooner rather than later.

Tagged as: , Comments Off
Comments (2) Trackbacks (0)
  1. You’ve missed several initialization possibilities for both java and c++ (not to mention the stuff the the new standard adds). What about copy and assignment? What about cloning? You’ve very seriously missed the on-going evolution of c++ as a language. Java pales in comparison to c++ and especially to the new c++. You can’t simply ignore the way a language is being changed, you are doing your readers and the industry a grave disservice and have relegated your ramblings to irrelevance. c++ does indeed have stronger reference semantics and they are getting stronger. Anyone that would argue this point isn’t qualified to argue it.

  2. @someone: You might see http://blogs.foognostic.net/2008/12/resolving-to-use-cpp-in-2009/ . In short, a.) my C++ feature exposure was conservative in the name of multiple compiler compatibility and b.) I have ignored the evolution of C++ for several years while languishing in Java. So I agree with your points there.

    My intent behind the idle ramblings was just to simply contrast the basics of reference types in Java and C++. This and other feedback on this post agree that it was too sparse on details. I guess I shouldn’t be so scared of the dreaded tl;dr.


Trackbacks are disabled.

Page optimized by WP Minify WordPress Plugin

Foognostic blogs is Stephen Fry proof thanks to caching by WP Super Cache