Thread: Pointers and Efficiency

  1. #61
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Not really. Pointers look natural to me. References look unnatural for the most because of their static nature.
    Then I guess you don't "get" them.

  2. #62
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by brewbuck View Post
    Then I guess you don't "get" them.
    Agreed.

  3. #63
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's because they're too static. I like dynamic and not static.
    All the time when I design something, references turn up useless all the time due to their nature.

  4. #64
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    That's because they're too static. I like dynamic and not static.
    All the time when I design something, references turn up useless all the time due to their nature.
    If you purposefully think in terms of pointers then obviously the only kinds of solutions which will occur to you are pointer-based. You want something "dynamic" because that's the way you're used to solving problems. That's fine, but it doesn't make references less useful.

    In C++ I typically view pointers as ugly and try to avoid them whenever possible. And that DOESN'T mean sacrificing efficiency or writing stupid code. There are GOOD ways to avoid pointers.

  5. #65
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since I usually have methods that take other classes, I don't use by value, and I can't use references because they need to be initialized, so I really don't see references as anything else but useless most of the time.
    I'm sacrificing them for my own savvy design with polymorphism and OO programming.
    But there are a few points where they might work, but even there, they're mostly useless because the same can be done with pointers and with or without pointers, there's no complexity in those needs.

  6. #66
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    This is almost like saying data types like float, char, etc. are useless because we can always use double or int respectively.

  7. #67
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not as I see it.
    I can just as easily require a T** instead of T*&, only requiring the user to do &obj instead of obj, which I find perfectly acceptable. Although, I may agree that this might also be the only use for references. Some functions might also require a local argument to perform work to (like a buffer). Passing a reference is acceptable, except you'll probably also end up putting that buffer on the heap and to my knowledge, there's no way to tell if an optional argument passed by reference has been passed or not. With pointers, it's easy, because you'll initialize them to NULL by default.

    I also used to pass some function with a return type by value due to that I did not want the responsibility to delete those objects. Now, I use pointers, encapsuled with a smart pointer class, that takes care of that bit. Passing back references is useless since it would just reference a local variable.

  8. #68
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Explain to me then why I need floats vs doubles any more than you need references vs pointers.

  9. #69
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I won't, because that's up to you. My style of programming makes references useless.

  10. #70
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Since pointers and references are being discussed, I just thought somebody could clear this up - which of following functions is more appropriate to use in C++? Or maybe there's a "better" C++ way of handling pointers/references passing/returning? Should I avoid returing pointers at all in C++?

    Code:
    int main()
    { 
        string modifyMe;
        modifyString1( modifyMe );
        // or
        string* modifyAlso = modifyString2();
     
        return 0;
    }
    
    int modifyString1( string& str )
    {
       str.assign( "Hello world" );  
    }
    
    string* modifyString2()
    {
        string* str = new string( "Hello world" );
        
        return str;
    }
    Sorry if this post should't belong here.

  11. #71
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jimzy View Post
    Since pointers and references are being discussed, I just thought somebody could clear this up - which of following functions is more appropriate to use in C++? Or maybe there's a "better" C++ way of handling pointers/references passing/returning? Should I avoid returing pointers at all in C++?

    Code:
    int main()
    { 
        string modifyMe;
        modifyString1( modifyMe );
        // or
        string* modifyAlso = modifyString2();
     
        return 0;
    }
    
    int modifyString1( string& str )
    {
       str.assign( "Hello world" );  
    }
    
    string* modifyString2()
    {
        string* str = new string( "Hello world" );
        
        return str;
    }
    Sorry if this post should't belong here.
    Actually, to make the to functions more equal I think your pointer version should look like this:
    Code:
    void modifyString2( string* str )
    {
        if ( str != NULL )
            str->assign( "Hello world" );
    }
    This shows one very nice things about references -- a reference can never be NULL, so if someone gives you a reference there's no need to check it for NULL.

    Another good thing about them is that they are static. If a reference is initialized at the top of a very long function, then you see it used somewhere near the bottom, you know that it is still pointing to the same object that it was initialized with. With a pointer you'd need to carefully check the whole function down the that point to make sure the pointer wasn't re-assigned to point somewhere else, or worse to point to NULL or a deleted object.
    So basically, the developer is required to maintain a lot more of the program state in their head when using pointers than they would using references; and on a friday afternoon when you're half asleep from working late all week, your mind could easily be a little low on 'cache' memory and you could overlook a pointer re-assignment.

  12. #72
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Make your own topic next time.

    For something as simple as assigning a string to something basic, you should consider just assigning it directly. Depending on what you're doing with the object, there are numerous ways of manipulating it. There may not be a "correct" way of doing it. Interesting to note, in your example, you neglected to delete the string you allocated with new. You might have left it out for brevity, however, for real programs, you have to remember to deallocate memory you allocate, or possibly suffer memory leaks. Because of this possibility, you should consider writing code that is not conducive to allowing memory to be leaked.

  13. #73
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jimzy View Post
    Since pointers and references are being discussed, I just thought somebody could clear this up - which of following functions is more appropriate to use in C++? Or maybe there's a "better" C++ way of handling pointers/references passing/returning? Should I avoid returing pointers at all in C++?

    Code:
    int main()
    { 
        string modifyMe;
        modifyString1( modifyMe );
        // or
        string* modifyAlso = modifyString2();
     
        return 0;
    }
    
    int modifyString1( string& str )
    {
       str.assign( "Hello world" );  
    }
    
    string* modifyString2()
    {
        string* str = new string( "Hello world" );
        
        return str;
    }
    Sorry if this post should't belong here.
    Depends on what you want to do. The "real" C++ way (like everyone else here suggests ), would be to take a reference to a string.
    If you want to return a string (instead of modifying it directly), consider asking if it's worth to pass the object by value. That way you won't have to delete it later. Of course, you can also use a smart pointer class and return that with a pointer that will clean up itself. Up to you.

    This shows one very nice things about references -- a reference can never be NULL, so if someone gives you a reference there's no need to check it for NULL.
    Doesn't mean the reference is bound to an allocated object or variable, though.

    Another good thing about them is that they are static. If a reference is initialized at the top of a very long function, then you see it used somewhere near the bottom, you know that it is still pointing to the same object that it was initialized with. With a pointer you'd need to carefully check the whole function down the that point to make sure the pointer wasn't re-assigned to point somewhere else, or worse to point to NULL or a deleted object.
    Then just declare those pointers const and assign them. But then again, you could never re-assign them again.
    As for NULL and things - that's not a pointer's only use. You can point to other object which does not necessarily be deleted. Very handy, for example, when you walk through a linked list. So you don't need those pointers to point to allocated objects which must be deleted. And if they do, I would use a smart pointer.

    So basically, the developer is required to maintain a lot more of the program state in their head when using pointers than they would using references; and on a friday afternoon when you're half asleep from working late all week, your mind could easily be a little low on 'cache' memory and you could overlook a pointer re-assignment.
    Not if you use them correctly

  14. #74
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elysia View Post
    Doesn't mean the reference is bound to an allocated object or variable, though.
    A reference is ALWAYS bound to an allocated object or variable, except when there is a mistake in their usage in the first place. That is, either you have a reference to an object which has a shorter lifetime than the reference (most prominently, the classic "return reference to local object" error), or you delete memory you still hold a reference to. Both should be considered errors in their usage.

    Then just declare those pointers const and assign them. But then again, you could never re-assign them again.
    Now you're just being contrary. Yes, const pointers have the same init-only semantics as references (without the lifetime extension semantics, but few people use those anyway), but references are syntactically more convenient.

    As for NULL and things - that's not a pointer's only use. You can point to other object which does not necessarily be deleted. Very handy, for example, when you walk through a linked list.
    Linked lists are one of those low-level jobs pointers are good for.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #75
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Doesn't mean the reference is bound to an allocated object or variable, though.
    I think "initialised" may be more accurate than "allocated" here, though in the case of objects of class types the object passed has been initialised, so there is no problem anyway.

    Effectively, this is not a comparison between which is better. Rather, this is a comparison between which is appropriate for the situation.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Improving the efficiency..
    By aaronljx in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 10:09 AM
  2. trouble with file pointers...
    By quasigreat in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 08:12 AM
  3. Function Pointers in C
    By Gobinath in forum C Programming
    Replies: 7
    Last Post: 03-10-2005, 11:54 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Question about efficiency... structures and stuff
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2002, 01:00 PM