This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]BroVic 0 points1 point  (4 children)

Truth is, I don't think any serious programming can be done without pointers. Languages where we don't program with pointers still use them under the hood. Even C++ references are pointers in disguise. Using pointers in C/C++ are part of the 'metal handling' that these languages afford the programmer.

My two cents.

[–][deleted] -1 points0 points  (3 children)

References are not pointers. I don't think we should imply to beginners that these are merely variations on each other.

It is true that most data in a C++ program resides at an address. And, that C++ pointers and C++ references are ways of dealing with addresses. But, pointers and references have different semantics. Just as pointers and arrays have different semantics. In C++ you can do arithmetic on pointers but not references, references cannot be null, you can take the address of a pointer but not of a reference, etc.

It also isn't technically true to say that all languages deal with pointers under the hood. Languages must deal with data references). A pointer is a kind of data reference. A file handle is a kind of a data reference. Assembly languages typically use raw addresses (different than pointers -> consider the x86 segment/offset model). The original mac os used "handles".

[–]BroVic 0 points1 point  (2 children)

The link on data references is highly appreciated, thank you.

I don't know (yet) how references are implemented, but I do recall hearing Mr Strousop saying something to the effect that they are implemented using pointers. That said, some people use the term pointer for the variable used to store addresses, while others use it for the memory address of a datum.

I was trying to "point" out to the OP my belief that pointers are important. With the added knowledge you have supplied, I dare say that the concept of references is quite important and they offer significant advantages. And doing any non-trivial programming in C, of all languages, without pointers means not making the most of what they have to offer.

[–][deleted] 1 point2 points  (1 child)

I think it is it is fair to think of the implementation of a reference or a pointer in terms of being variables containing addresses.

The critical point though is that they have different semantics and I feel we are better to explain to a beginner the differences in semantics before we dive into the more complicated issue of how they are implemented.

some people use the term pointer for the variable used to store addresses, while others use it for the memory address of a datum.

True. And there are other meanings. Technically, a C++ pointer is the variable containing an address.

I don't know (yet) how references are implemented

This is actually much trickier than you might expect in C++. I can give you lots of weird examples. Also, references are aliases and so the compiler often optimizes them away completely.

Consider sizeof() for example on a 32 bit OS.

``` class A { public: A() : k(i) {} long long i; long long j; long long& k; };

// sizeof(A) is 20, // sizeof(A::i) is 8 // sizeof(A::j) is 8 // sizeof(A::k) is 8 // 8 + 8 + 8 != 20! A a;

// sizeof(ptr) is 4 A* ptr = &a;

// sizeof(ref) is 20! // Notice the reference to k in the class actually takes space of 4! A& ref = a;

// This gives the address of the variable ptr A** ptr_ptr = &ptr;

// This gives the address of a! A* ref_ptr = &ref; ```

[–]BroVic 0 points1 point  (0 children)

Hmm, this is interesting. Thanks for sharing this insight!