Jump to content

Is a reference a constant pointer?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Hey guys, I'm currently reading a book "Beggining C++ Through Game Programming 3rd Edition" and the author states that a reference is like an alias. Later in the book when he shows how to use pointers he states in a hint message that references should be used instead of constant pointers due to a cleaner syntax. My real question is are they the exact same thing, and if so what do you prefer. Honestly I hate syntax sugar because it is just another thing for someone to remember about a programming language. Also I read this article: An Insight to References in C++ - CodeProject, and it clearly shows that they are the same thing. However many replies were stating that references dont take up any space in memory like constant pointers. I know this is incorrect because it is impossible to have an alias in binary without storing.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
They are similar, but they are NOT the same thing. A reference is a variable that uses the same memory location as another (already existing) variable.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The size of a pointer will tend to be 32 bits (4 bytes) assuming 32 bit platform of course, as that will allow it to point to all possible 32 bit memory addresses. References are only taking up space before the compiler (i.e. the physical & symbol), the compiler will resolve and connect the references automatically so it does not behave like a pointer.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
@NullW0rm The only thing that gets me is this code snippet.

 Collapse | Copy Code

#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;

    return 0;

}

From my understanding sizeof() is a unary operator that returns the size of the argument they are given. In this case each reference is returning 4, which I'm assuming means 4 bytes. Although I could be wrong because references are supposed to be initialized on creation and if this reference isn't being initialized at all it could be acting differently then a reference that is pointing somewhere.

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Your reference is 0, your integer of the reference is 4 bytes. A reference does not exist in execution time and therefor has no existance. Knowing the reference the compiler can generate the exact same code when a reference is used just as when the thing it refers to is used.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
espdev-darkness

espdev-darkness

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
Okay, thanks for explaining that. I will try to use references whenever I can.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users