DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: References and Pointers

  1. #1
    Join Date
    May 2004
    Posts
    242

    References and Pointers

    Hello,

    vector<double> vec1;
    vector<double> &ref1 = vec1;

    I know that ref1 is a reference to vec1 and that anything I do to ref1 is the equivalent to doing the same thing to vec1, and visa versa.

    Similarly, the same works with pointers.

    However, I'm not quite sure what is the advantage for using these because I can always work on the original object vec1, so why bother with these references and pointers at all ? Is it to save memory especially when copying by value, instead copy be references or pointer ? Are there any other advantages ? Once I was told that copying be pointer/reference we decrease the chances for memory leaks if we do multiple copies. How/why ?

    Thanks,

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,
    A reference is very much similar to a pointer in that it stores only the address of an object. The main difference is, that this object has to *exist*. That means a reference can never be NULL (0), whereas a pointer can be. That means as well, that a reference variable *has to be initialized upon declaration* (as you do in your example). The beauty of references is, that you do not have to bother about allocation/de-allocation of objects. You always know, whoever does the memory management of an object your reference refers to: It isn't you. So you can use the efficiency and dynamicity of pointers without the *ugly* memory management issues.
    Hence you lower the risk of leaks, etc.

    I bet I missed out half of it, but I still hope that helps.

    Cheers,

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    I found that .net (at least 2002 version) does NOT optimize references. It puts a true pointer in, making the code less efficient than just using the actual object. In theory, it should map both names to one memory location, and it would be as efficient to use either name.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    The optoimization stuff is very platform dependent. In mot cases, it's impossible to optimize away the reference because it is used in several places, each of which may be compiled seprately. Normally, one doesn't use references as aliases anyway, but as an efficient argument passing mechanism. The restrict pointers of C99 and the register passing mechanism enable the compiler to eliminate redundant copies of the same reference or pointer but not all compilers support these features.
    Danny Kalev

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by ami
    However, I'm not quite sure what is the advantage for using these because I can always work on the original object vec1, so why bother with these references and pointers at all ?
    Thanks,
    Mostly, you use references for passing arguments to functions. Notice that when you pass vec1 to a function, you're actually passing a copy of that object. So if the function needs to access vec1 itself, you must pass it as a reference or a pointer.
    On a related note, pass by reference is also more efficient and saves memory because it avoids object reduplication.
    Danny Kalev

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    When I discovered it I was trying to name array locations via a reference but the extra pointer cost forced me to back away and use array[enum] format instead. It would have been nice to use a reference for that, but the enum names are readable enough.

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by jonnin
    When I discovered it I was trying to name array locations via a reference but the extra pointer cost forced me to back away and use array[enum] format instead. It would have been nice to use a reference for that, but the enum names are readable enough.
    Ho wdid your original reference declaration look? I think arrays are a bit different because their names are rvalues.
    Danny Kalev

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    int & named = array[location]; //the assembler for this used a pointer, not direct access to the int, which sort of makes sense but it *could* be optimized away since the memory offset can be determined at compile time. But after that I checked it with a simple

    int x;
    int &y = x; //again with the pointer, which is 100% not needed here. This should simply share the address at the assembly level without a pointer at all.

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    I'm not sure that using an extra variable that contains the address is less efficient. It does take up more memory but if the variable is stored in a register, it's pretty fast. OF course, the address itself could be stored without using another variable, i.e. &array[location] but it amounts to the same thing, doesn't it?
    Danny Kalev

  10. #10
    Join Date
    Dec 2003
    Posts
    3,366
    the pointer dereference is several more operations to get the actual value that you wanted. I was sort of hopeing references were exactly that - a new name for the same location with *nothing* being actually *done* to make them happen because the compiler would map the bound reference and original variable to the same place -- the same exact assembler would be generated for either name of the item. This is not the case, it loads the pointer, loads the offset, finds the memory location and fetches that back to the register, instead of just putting the value in the target register. Parameter references are different, as they are passed in on the stack and its the same amount of work in assembler no matter what you do.

    And, its probably not an important number of extra instructions for most programs. Many of the things I have to worry about don't really matter for most programs. If we had an infinite budget, I could probably ignore some of them too =)

  11. #11
    Join Date
    Nov 2003
    Posts
    4,118
    I think what you're referring to i.e., a new name for the same location, was implemented in Algol in the late 1950s. It was called pass by name. At first, it seemed ingenious indeed since there was no pointer math involved, and certainly no reduplication of variables. However, later it was discovered that pass by name created subtle bugs in some cases such as swapping. That's why no other programming language has implemented this passing mechanism since. So I suspect that this is the problem here too. Admittedly, in some cases the compiler can optimize away the extra pointer used as a reference and I believe that that's what restrict pointers in C99 do. However, as a general optimization technique this can be dangerous so compilers avoid it.
    Danny Kalev

  12. #12
    Join Date
    Nov 2006
    Posts
    12
    Mostly, pointers and references are used for passing variables (int, char, float, or any kind of variable or object) by reference to functions, so that changes made to them inside the function are passed back to the calling function.
    i.e
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void insert_into_vector(vector<double> & vRef);
    
    int main()
    {
    	vector<double> v;
    	insert_into_vector(v);
    	for (int i=0; i<v.size(); i++)
    		cout << v[i] << endl;
    	return 0;
    }
    
    void insert_into_vector(vector<double> & vRef)
    {
    	for (int i=0; i<10; i++)
    	{
    		vRef.push_back(i);
    	}
    }
    In this code, changes to v (referenced as vRef) in the function insert_into_vector are passed back and when you display v in the main function it will have the values inserted from the insert_into_vector function!

  13. #13
    Join Date
    Jun 2006
    Location
    Zagreb
    Posts
    49
    Cairo... that's in Egypt
    aliens are friends

Similar Threads

  1. Project references to Type library (.tlb) files
    By Heather B. in forum VB Classic
    Replies: 10
    Last Post: 05-14-2001, 04:07 PM
  2. Project references to Type library (.tlb) files
    By Heather B. in forum VB Classic
    Replies: 0
    Last Post: 05-10-2001, 07:42 PM
  3. Flaw in C# ?
    By Gunnar Skogsholm in forum .NET
    Replies: 20
    Last Post: 01-17-2001, 07:43 PM
  4. Pointers to good exchange references
    By Alex in forum Enterprise
    Replies: 0
    Last Post: 11-30-2000, 06:03 PM
  5. Interfaces, hidden windows and fixed string pointers
    By Lobo in forum authorevents.appleman
    Replies: 5
    Last Post: 04-12-2000, 03:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center