On the Pointers and References

A Reference is a distinct concept in C/C++ languages. The next code sample (MinGW GCC compiler was used)

#include <iostream>

int main()
  {
    int Value;
    int &ValueRef = Value;

    Value = 2;
    std::cout << "Value: " << Value << "\n";
    std::cout << "ValueRef: " << ValueRef << "\n";

    ValueRef = 3;
    std::cout << "Value: " << Value << "\n";
    std::cout << "ValueRef: " << ValueRef << "\n";
    return 0;
  }

declares ValueRef as a reference to Value variable. The output is
_ref1
Though ValueRef variable is a pointer to Value internally the indirection operator is never used, and the syntax for accessing Value directly or indirectly via ValueRef is the same. ValueRef is also called an alias to Value; the point that ValueRef variable is a pointer internally is just an implementation detail.

Another important thing about C/C++ references is that they are always initialized. The language syntax enforces that you cannot declare a wild reference.

Pascal does not have the same reference concept. The closest concept is a procedure parameter passed by reference:

program ref;

{$APPTYPE CONSOLE}

var
  Value: Integer;

procedure Test(var ValueRef: Integer);
begin
  Writeln('ValueRef: ', ValueRef);
  ValueRef:= 3;
end;

begin
  Value:= 2;
  Writeln('Value: ', Value);
  Test(Value);

  Writeln('Value: ', Value);
  Test(Value);
end.

we can see that

  • ValueRef does not use indirection operator to access a referenced variable;
  • the language syntax enforces that ValueRef is always initialized.

Delphi does not elaborate the reference concept, though there are many built-in types in the language that are ‘transparent pointers’ – objects, interfaces, dynamic arrays, strings. The term reference can be used for example for object variables because the language syntax makes these variables indistinguishable from referenced instances. Instead the term object is usually used, that can mean object reference or object instance, so sometimes you think twice to understand what a particular object does mean.

Advertisements

6 thoughts on “On the Pointers and References

  1. You can make an equivalent code in delphi using the absolute directive. This result in a variable that resides at the same address as another variable.

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils;
    
    var
      value: Integer;
      valueRef: Integer absolute value;
    begin
      value := 2;
      Writeln('value: ', value);
      Writeln('valueRef: ', valueRef);
    
      valueRef := 3;
      Writeln('value: ', value);
      Writeln('valueRef: ', valueRef);
    
      ReadLn;
    end.
    
  2. @Fabricio
    You are perfectly right!
    I use “absolute” sometimes, especially when I work with untyped const and var arguments, instead of pointers. Or when I do not want to use a “variable record”, e.g. since its internal alignment has been broken (changed?) with Delphi XE3.

    As far as I understand the Delphi roadmap, the “absolute” keyword is about to disappear, perhaps in the NextGen compiler.

  3. Your assertion is 100% incorrect, Delphi does infact have exactly the feature you are describing. It is the “absolute” keyword and it has been part of Object Pascal for a *LONG* bloody time.

    var
    Value: Integer;
    ValueRef : Integer Absolute Value;

    begin
    Value:= 2;
    Writeln(‘Value: ‘, Value);
    Writeln(‘Value: ‘, ValueRef);

    Value:= 3;
    Writeln(‘Value: ‘, Value);
    Writeln(‘Value: ‘, ValueRef);

    end.

  4. Oh, and it has been suggested that Absolute was going to disappear ever since Delphi 8.

    Hasn’t happened, and probably never will – the feature requires near zero maintenance in the compiler. They would break code, piss people off and not reduce compiler code complexity in any significant way, and when you need it, it can do things that typecasting just isn’t capable of.

    It’s not like the WITH keyword.

  5. Actually, Allen Bauer has said very clearly that absolute is on his list of things which must be removed, in order for the Delphi compiler to make some major gains. So yes, it’s been warned about for years, but I think it’s about to become a reality.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s