Hi all,

I was reading about some stuff about References in C++ and got into a debatable topic..

Is references same as a constant pointer to a variable or not.

I got some info. to support the topic here.

At the same time, something that says otherwise here.

Can you put in some light on the subject ?

Recommended Answers

All 11 Replies

1. References are similar to pointers. But not same. That's why the std committee came up with 2 different things. :).
2. "references same as a constant pointer to a variable" => Yes, a reference and a const ptr to a var would work "almost" same way (you'll use different operators "." vs. "->")
Mind you this is just a way of explaining references.

Basic differences between a pointer and reference are:
1. A ptr can be reassigned, a ref can't be.
2. A ptr could be left uninitialized, a ref can't be.
3. To access a member: on a ptr you use "->" whereas on a ref you use "."
4. Access to member thru ptr would mean an extra level of dereferencing (compared to access thru ref)

No, What i wanted to ask was about their storage in the memory rather than their usage.

The link i sent in my last post suggests that internally reference are treated as const pointers only.
That is, the reference in itself holds a separate memory location other than the variable it is referring to , rather than acting as an alias to the variable as the notion is usually.

Ofcourse the size of the class suggests likewise as shown below:

#include <iostream>
using namespace std;
class Test
{
    int &i;   // int *const i;
    int &j;   // int *const j;
    int &k;   // int *const k; 
};
int main()
{    
    // This will print 12 i.e size of 3 pointers
    cout<<  "size of class Test = "  <<   sizeof(class Test)  <<endl;
    cin.get();
    return 0;
}

But is it always...or is it compiler dependent...??

No, What i wanted to ask was about their storage in the memory
But is it always...or is it compiler dependent...??

From std: sizeof operator: "When applied to a reference or a reference type, the result is the size of the referenced type. When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array."
Of course this doesn't say that sizeof class = (sum of size of all members)
I couldn't find any mention of what should be the size of a reference or pointer (or how they should be stored in memory).
AFAIK: A pointer variable has it's own memory and value (the adr) and to access what it points one has to first read the adr and deref it. sizeof(ptr type) is always same on one architecture irrespective of type.
Whereas a reference to some variable would act like an alias and would not require any extra memory. The name of the reference would be stored just the way name of any other variable will be stored, just that this name will be associated with same memory as variable it refers to.

except in a trivial situation, where the compiler can optimize away the storage, a reference is stored in memory as an address. so for, struct A { double& ref ; A(double&d) : ref(d) {} }; sizeof(A) == sizeof(double*)
when a reference is passed to a function, what is passed is the address. it is easy enough to check this out

cfile.c
------------

#include <stdio.h>

void c_function( double* const pointer )
{
  printf( "c_function:: pointer: %p\t*pointer: %f\n", pointer, *pointer ) ;
  *pointer += 333.444 ; 
  printf( "c_function:: pointer: %p\t*pointer: %f\n", pointer, *pointer ) ;
}

references.cpp
------------------------------

#include <iostream>

// cheat on the type; exploit type unsafe linkage of C
// extern "C" => linkage is "C", type system is still C++
extern "C" void c_function( double& dbl ) ;

int main()
{
  double value = 1.234 ;
  std::cout << "main:: &value: " << &value << "\tvalue: " << value << '\n' ;
  c_function(value) ;
  std::cout << "main:: &value: " << &value << "\tvalue: " << value << '\n' ;
}

> gcc -Wall -std=c89 -c cfile.c
> g++ -Wall -std=c++98 references.cpp cfile.o
> ./a.out
main:: &value: 0xbfbfe950 value: 1.234
c_function:: pointer: 0xbfbfe950 *pointer: 1.234000
c_function:: pointer: 0xbfbfe950 *pointer: 334.678000
main:: &value: 0xbfbfe950 value: 334.678

this would be the case with all C++ compilers

Ya , but if a reference is stored as a separate memory location, i.e it implicitly functions as a constant pointer to another variable, then how come displaying the address of the reference as well as its referent shows the same address ?

... i.e it implicitly functions as a constant pointer to another variable, ...

except at point of initialization, it is like a constant pointer to another variable that is implicitly dereferenced.

int i = 57 ;
int& r = i ;
int* const p = &i ;
r == *p == i ;
&r == &(*p) == &i ;
sizeof(r) == sizeof(*p) == sizeof(i) == sizeof(int) ;
typeid(r) == typeid(*p) == typeid(int) ;

>sizeof(r) == sizeof(*p) == sizeof(i) == sizeof(int)

That i believe is only in the case of an int..

What about &r and &p ?

>What about &r and &p ?

That's what I was trying to say when I said: "Whereas a reference to some variable would act like an alias and would not require any extra memory. The name of the reference would be stored just the way name of any other variable will be stored, just that this name will be associated with same memory as variable it refers to."
==> Means
- &r is same as &i
- &p is NOT same as &i

yes.. but then you actually contradict vijayan121.

Personally i also had the same notion until i read the tutorial referred in my first post.

That brings it back to its initial state...So who is right actually...
We need to agree upon something right..

>I got some info. to support the topic here.
>At the same time, something that says otherwise here.
I don't see the contradiction. Both articles say the same thing, except cprogramming takes a programmer's perspective and Code Project takes an implementor's perspective.

>Is references same as a constant pointer to a variable or not.
Yes and no, depending on how deep you want to go. At the top level where references are actually used, they're not and shouldn't be viewed as pointers that are automatically dereferenced. They're references; aliases for the object that they reference. At the lowest level there's no such thing as references, only addresses. At that level, references have to be implemented as pointers (assuming no tricks) because there's no other option for that kind of indirection.

I think it's the abstraction that's confusing you. Code Project is wrong in that C++ does indeed support pass by reference. If you follow their logic then C++ doesn't support templates either because under the hood it's just a glorified preprocessor. Or polymorphism because it's just an array of pointers. The whole premise for their argument is unsound.

cprogramming is more accurate and describes the whole feature better. The key reason is that Code Project tries to explain the implementation of something that's not required by the standard. Compilers don't have to produce pointer code to implement references.

I believe this is what i was looking for.. i.e a sound stand on some grounds ( either of the two).

Actually i was preparing for an interview and reading both the stuffs actually made me think on which one to actually accept.

Thanks 4 dat..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.