0

I've learned programming in Java at university. The book I read, explained the concept of pointers being used for object variables. Later I checked the actual concepts of pointers & references in C++.

I know these aren't 100% applicable to other high level / scripting languages, but there are recurring patterns that are similar.

When I started learning new languages, I noticed that most of them rely on the concept of storing some sort of object identifier inside a variable. When using an accessing-operator on the object-variable, it will read it, identify the object based on the identifier and access its content. I'd say this comes pretty close to the basic idea of a pointer.

I've confirmed that behavior for PHP and JS. Example for PHP: https://www.php.net/manual/en/language.oop5.references.php

When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

However, I often see people mixing up the terms reference and pointer. This started confusing me a little.

Personally, I think pointers are a more elegant way to store objects.

I'd like to know if pointers are actually a common case as object-variable or if references are just as common. Do pointers have notable advantages over references?

It's hard to tell which general concept is being used, because from a programmers perspective, the internals are often abstracted away. But I'd like to have a rough understanding of what's happening under the hood. Not in all detail, but conceptually.

tweekz
  • 187
  • 1
  • 9
  • *"I'd like to know if pointers are actually a common case as object-variable or if references are just as common."*. For which programming language? `javascript`, `php`, `c++`, or `Java` as you mentioned in your question? Also, what does *"a common case"* mean? In `c++`, you can have both pointers and references to variables. – Ryan Zhang Aug 12 '22 at 13:14
  • 1
    While learning C++, forget just about *anything* you know from Java as a start. **Especially** Java's memory management. – Michael Chourdakis Aug 12 '22 at 13:15
  • A pointer in c or c++ is used completely different than a reference. All the dereferencing has to explicitly be handled by you as a developer. That to me is the difference. The only difference that is of interest. It offers a lot of power but also a lot of risk, obviously. If you instead focus on whether this or that language _internally_ uses a pointer or a reference to handle references - why would that matter? Such implementation detail should indeed be hidden from users (developers) as much as possible. Knowledge about such details only leads to miss usage and therefore to problems. – arkascha Aug 12 '22 at 13:16
  • In most languages I've learned so far, it seems to use some sort of pointer for object-variables. I'd like to know if this is just coincidence or if it holds some serious advantages over references? – tweekz Aug 12 '22 at 13:18
  • 1
    In JS there is no pointers and references works diferent than in c++ so sadly you will need to understand this for every language – Edoldin Aug 12 '22 at 13:19
  • @tweekz FYI -- Look through the C++ specification for "reference". You will not see anywhere on how references are implemented. All you will find is how the language is supposed to behave when a reference-type is used. – PaulMcKenzie Aug 12 '22 at 13:24
  • 1
    *In most languages I've learned so far, it seems to use some sort of pointer for object-variables.* This is one way (among many) that C++ is different from other languages. Object variables in C++ are not pointers or references, they are values. This fact must be the number one reason Java programmers struggle when they switch to C++. – john Aug 12 '22 at 13:24
  • @john You're right, but most other languages generally adopt one of those concepts. – tweekz Aug 12 '22 at 13:27
  • @tweekz -- C++ isn't Java, and conflating "references" between the two languages will only add confusion. When a Java programmer sees this: `void foo(Widget w);`, the Java programmer calls the first one "pass-by-value", but wait -- the object `w` is the same one the caller sent over -- that concept is "pass by reference" in C++, not "by value", so the call should be `void foo (Widget&)`. That's why when a Java programmer writes C++ code, and they do `foo(Widget w)`, they are surprised that `w` has not changed when `foo` returns. This is the danger of writing C++ code using Java as a model. – PaulMcKenzie Aug 12 '22 at 13:38
  • 1
    To add to the confusion, languages may use different terminology from one another. What C++ calls a pointer, Java calls a reference. The terminology has to be understood in context. – Eljay Aug 12 '22 at 13:58
  • 1
    @Eljay I think that's what caused some confusion with my question. When I say pointer, I actually don't mean a C++ type pointer, which you can manipulate, but rather the concept of a variable storing some sort of address/identifier used for accessing the object. A reference on the other hand - in my words - is just an alias for the same variable. – tweekz Aug 12 '22 at 14:54
  • 1
    Agreed. Since there is no standard terminology for these things in computer science, there's a need to construct a meta-terminology for the discussion that abstracts the different language-specific jargon into a common nomenclature. Almost guaranteed to cause a Mister Misty headache, except for the OCD or those with a linguistics background. – Eljay Aug 12 '22 at 14:59

1 Answers1

1

At the CPU level, there are pointers (and nothing else except the registers). Therefore, all so called low-level languages must have some pointer mechanism because that's how the internals work. Every variable, function, etc is a pointer at the assembly level.

At the language level however, it's a different story if and how pointers and references are used. If they are implemented in similar way, that's up to the implementation. At the language level, they are different. So you need to learn both. It's not only the concept of a pointer or a reference also, it's the whole process of memory management, variable visibility and lifetime etc.

Suggestion: Start cleanly with a good C++ book and don't take assumptions on anything. Java's memory management is quite different from C++ and C++ 11 and later memory management is also different than earlier versions.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • My idea was that there could be general technical reasons, why to apply this concept on a more abstracted level as well. Like, when having dynamically assigned objects, it seems easier to me to handle those references stored as value in pointers. – tweekz Aug 12 '22 at 13:25
  • If you are familiar with internals, it will be easy to reduce language aspects to the Cpu level. – Michael Chourdakis Aug 12 '22 at 14:12
  • I think my question caused some confusion for the wording. I know that high-level languages don't match what's exactly going on under the hood. But it seems like there are recurring patterns. Like variables that store identifiers for accessing objects - that's what I called a pointer, as it's a variable pointing to an object. References on the other hand I regard as aliases. – tweekz Aug 12 '22 at 14:57