Thread: I need an explanation about references

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    9

    I need an explanation about references

    Hello, i know some of the basic stuff about references...but what's going on here:

    Code:
    #include<iostream>
    
    using namespace std;
    
    void foo(int& aa,int& bb){
    
        aa=0;
        bb=0;
    
    }
    
    int main(){
    
    
    int a(1);
    int b(1);
    
    foo(a,b);
    
    cout<<a<<" "<<b<<endl;
    
    return 0;
    }
    When i call foo function, does it push its arguments onto the stack? What is the layout of a stack frame? What is the difference between pass by reference and pass by a pointer?

    I read somewhere that there are no references to references, but i compile this:

    Code:
    #include<iostream>
    
    using namespace std;
    
    
    int main(){
    
    int a(0);
    int& b=a;
    int& c=b;
    
    
    return 0;
    }



    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by 4c0 View Post
    When i call foo function, does it push its arguments onto the stack? What is the layout of a stack frame? What is the difference between pass by reference and pass by a pointer?
    You could look at the stack frame:
    Code:
    #0 00000000	foo(aa=@0x28fefc: 1, bb=@0x28fef8: 1) (C:\Users\Josh2\Documents\bar\bar2.cpp:7)
    #1 004013B0	main() (C:\Users\Josh2\Documents\bar\bar2.cpp:18)
    Clearly something is being passed in as an argument. It is functionally equivalent to a pointer, if that isn't confusing. References are a way to pass an object to a function without worrying about copying it or NULL references from a pointer.

    I read somewhere that there are no references to references, but i compile this:
    That is reference assignment, which you can do. It creates two references to the same object. In the code you posted, you have the reference to int b, and reference to int c both referencing the integer a. It's not exactly the same as a pointer to a pointer. In that sort of situation, you have a pointer value, which at that memory location also stores a pointer value.
    Last edited by whiteflags; 03-03-2013 at 07:02 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by 4c0 View Post
    When i call foo function, does it push its arguments onto the stack? What is the layout of a stack frame?
    Does it matter? All you need to know is what it does.
    These things are not really defined in the standard and will vary between different platforms and compilers, so there is no single answer to this question. So again, what does it matter?

    What is the difference between pass by reference and pass by a pointer?
    A reference is simple an alias (a different name) for a variable you pass in. So you are referring to a variable you passed in, but you've named it differently.
    A pointer passes the address of a variable, which you can then use to write and read to/from the address specified in the variable (and obviously change the address (value) stored inside the variable, too).

    I read somewhere that there are no references to references...
    References to references are illegal. Consider this:
    Code:
    int main()
    {
    	int x;
    	int&&& z = x;
    }
    This gives a compile error:
    error C2529: 'z' : reference to reference is illegal

    As to why there are 3 &, it's because && is also a type of reference (r-value reference); thus, it's legal, while &&& is not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    These things are not really defined in the standard and will vary between different platforms and compilers, so there is no single answer to this question. So again, what does it matter?
    https://www.google.com/search?q=practical+knowledge

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by 4c0 View Post
    I read somewhere that there are no references to references, but i compile this:
    Code:
    #include<iostream>
    
    using namespace std;
    
    
    int main(){
    
    int a(0);
    int& b=a;
    int& c=b;
    
    
    return 0;
    }
    That is not an example of references to references. b and c are both alternative names for (or references to) the variable a. b is a reference to a. c is a reference to the same variable as b (i.e. to a). It is not a reference to a reference.


    Quote Originally Posted by Elysia View Post
    As to why there are 3 &, it's because && is also a type of reference (r-value reference); thus, it's legal, while &&& is not.
    r-value references are only valid in C++-11. Not in earlier versions of C++.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    Unless you are optimizing for a system, that's not necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You're wrong.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    *shrug*
    I can be wrong. It doesn't matter to me.
    But I'm also not going to believe it has any practical purpose unless you are dealing with such a low level unless someone can prove otherwise.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I agree with elysia here. I have found, in my own experience, that knowing the details of the implementation can be more of a hindrance than a help. with such knowledge, a person is more likely to make assumptions, which may not hold true on all systems.

    it is much better to write standard-compliant code, and have faith that the developers of the platform/compiler know what they're doing.

    if the intent is to write code at a low level, such as an OS kernel or a compiler, it may be necessary to understand the implementation of such things, but in the overwhelming majority of other cases, the compiler knows better than you do.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    So does references take up memory space when i use them as with parameter passing? It depends on the implementation?

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It most certainly depends on the implementation.

    Section 8.3.2 paragraph 4 of the most recent draft C++ language standard:
    4 It is unspecified whether or not a reference requires storage (3.7).
    This was true in earlier C++ standards and it is that way now.

  12. #12
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    I only recently started learning C++ however from what I know up till now I tend to look at C++ "passed-by-reference" as basically const pointers. They both accomplish the same thing but one requires less code. Doesn't mean pointers cannot accomplish the same thing
    Code:
      1 #include<iostream>
      2 using namespace std;
      3 
      4 void foo(int& a,int& b){
      5 
      6     a=0;
      7     b=0;
      8 }
      9 
     10 void foo_ptrs(int* a, int * b){
     11 
     12    *a = 0;
     13    *b = 0;
     14 }
     15 
     16 int main(void){
     17 
     18    int a = 1, b = 1;
     19 
     20    foo(a,b);
     21    cout << "using foo() a = " << a <<" b ="<< b <<endl;
     22 
     23    foo_ptrs(&a,&b);
     24    cout <<"using foo_ptrs() a = " << a << " b = " << b << endl;
     25 
     26    return 0;
     27 }
    A reference does the same thing as a pointer in that it allows you to access the address of a variable and change it from within function scope. However
    It is supposed to be "safter" in that you cannot change the value it is pointing to. Whereas in C you can. C++ folks will argue its merits but for now
    I simply use it and see it as
    Code:
    const int* ptr
    or
    Code:
    int const *ptr
    .

    Always opened to hear more from those who've been working with it longer though. I'm still a C fan boy at heart
    Last edited by hex_dump; 03-04-2013 at 10:38 PM. Reason: posted wrong code

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hex_dump
    It is supposed to be "safter" in that you cannot change the value it is pointing to.
    And it is illegal to obtain a "null reference", whereas it is legal to pass a null pointer, hence the function must check for it or just declare the behaviour to be undefined.

    Quote Originally Posted by hex_dump
    I simply use it and see it as
    Code:
    const int* ptr
    or
    Code:
    int const *ptr
    I think you mean:
    Code:
    int * const ptr
    Last edited by laserlight; 03-04-2013 at 10:59 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Choosing to use a pointer in C++ usually means that you need something a reference doesn't support. References can't be NULL, and references can't be reassigned. This makes references a poor choice in certain situations. If a parameter is optional, a pointer might be preferred, since you can pass NULL. It's the same in the opposite way. If a parameter is required, using a reference would enforce the requirement. The compiler would have to check all of the arguments, and a reference can only be created with a live object.

    Pointers are also used to manage dynamic storage, but they are usually encapsulated in objects called "smart pointers," so you can reference those instead.

    That said, hanging references isn't impossible in C++:
    Code:
    int *p = new int;
    int& pref = *p;
    delete p;
    It does tend to be rather silly if you manage it though. It's exactly like using variables out of scope, and conventional C++ tends to follow the rules of scope, as a foundation for memory management, for instance. That's the real reason why C++ users say it is safe.

    In order to conceptually understand references, you might initially think that they are pointers. I think this is appropriate. But just as quickly, you will want to discard that idea, as they are separate types with different capabilities. Once you understand the different capabilities, your initial thoughts are actually quite wrong.

  15. #15
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    I think you mean:
    Code:
    int * const ptr
    Code:
    #include <stdio.h>
    
    int main(void){
    
       int a = 5, b=11;
       const int* ptr1 = &a;
       int const *ptr2 = &b;
    
    
       printf("ptr1=%d ptr2=%d \n", *ptr1, ++*ptr2);
       return 0;
    }
    I get this
    Code:
    cc -Wall some.c -o some
    some.c: In function ‘main’:
    some.c:10:4: error: increment of read-only location ‘*ptr2’
    meaning the compiler accepted it. My brain shuts down sometimes at night so...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using references and *this?
    By black_stallion in forum C++ Programming
    Replies: 6
    Last Post: 03-07-2012, 01:23 AM
  2. using references (need help)
    By dhardin in forum C++ Programming
    Replies: 9
    Last Post: 12-29-2009, 05:07 PM
  3. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 08:14 AM
  4. Help with references!!!
    By xeneize in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2002, 03:46 AM