CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 38
  1. #16
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: pointers--when to use them

    Quote Originally Posted by exterminator View Post
    Quote Originally Posted by HighCommander4
    Quote Originally Posted by Lindley
    In addition to NULLability, pointers have one other advantage over references: They can be reassigned.
    Not to mention pointer arithmetic
    Ok.

    Do you mean to say that what can be achieved in C++ with pointer arithematic cannot be achieved in say C#/Java where you don't have pointers and hence pointer arithematic?
    No, I mean to say that what can be achieved in C++ with pointer arithmetic cannot be achieved in C++ with references. Clearly Lindley was talking about C++ references, since Java/C# references can be reassigned.
    Old Unix programmers never die, they just mv to /dev/null

  2. #17
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: pointers--when to use them

    Quote Originally Posted by laserlight View Post
    I did not talk about objectives. Rather, I talked about pointer arithmetic. You simply cannot "increment" or "advance" a reference, or "subtract" references in C# and Java.
    Well, so did I but I got dragged into it.

    I only mentioned 2 ways to model something. There could be a disjoint set of baggage that comes along with those different models. So, comparing them was not my point. Ultimately, what matters is how you are using something to achieve the application requirements. It might have been an understatement on my part to say that there was only one difference but that was used as an example to illustrate a more general point I was putting up.

    Trying to modify the way C++ references work in C++ in any way is not a trivial task to do, it would need lots of thoughts and is a hypothetical case. I was just merely stating that pointers or references are just different ways to implement a concept: "address of an object". There might be nitty-gritties involved that are peculiar to one particular implementation. If both implementations were the same, why would you need 2 different implementations? That would be an overhead, we could easily work without.

    But if you ask me what is the most important different between a reference and a pointer in C++: I still maintain the view that NULLability is the one. But that is just my view and people may differ.

  3. #18
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: pointers--when to use them

    Quote Originally Posted by exterminator
    But if you ask me what is the most important different between a reference and a pointer in C++: I still maintain the view that NULLability is the one.
    Yes, I believe many of us here agree with you since it is one of the most often cited reasons for choosing a reference parameter over a pointer parameter when a newbie asks about how to make such a decision.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #19
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: pointers--when to use them

    Quote Originally Posted by HighCommander4 View Post
    No, I mean to say that what can be achieved in C++ with pointer arithmetic cannot be achieved in C++ with references. Clearly Lindley was talking about C++ references, since Java/C# references can be reassigned.
    Yeah, alright. Sure. I already accepted it would have been an understatement on my part if I sounded like : nullability was the only different between C++ pointers and C++ references.

  5. #20
    Join Date
    Apr 2009
    Posts
    21

    Re: pointers--when to use them

    Thanks everyone, this has been very useful. Originally, I thought that simply using the variable name again would suffice but I had been making 2 assumptions:
    1. small-scale programs vs. 100+ object programs
    2. I hadn't realized the benefits of pointer arithemtic

    Thanks again

  6. #21
    Join Date
    May 2009
    Posts
    2,413

    Re: pointers--when to use them

    Quote Originally Posted by exterminator View Post
    int& ref = *(new int(0));
    Hopeless Nerd: Look at me, look at me, I'm a genius,

    int& ref = *(new int(0));

    Team Leader: Use a pointer variable for heavens sake, or I'll assign you to the Java team where you cannot **** up.

  7. #22
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointers--when to use them

    That particular example is probably a bad idea.

    However, good C++ programmers will always prefer references over pointers when all else is equal, since they're, indeed, harder to **** up.

    That's what it always comes down to at the end of the day. Write your code in whatever style makes it hardest to do something stupid, and easiest to see at a glance what's going on.
    Last edited by Lindley; May 28th, 2009 at 04:13 PM.

  8. #23
    Join Date
    Aug 2007
    Posts
    858

    Re: pointers--when to use them

    Quote Originally Posted by Lindley View Post
    That particular example is probably a bad idea.
    I'd call it a pretty terrible idea TBH:

    Code:
    struct Foo
    {
      int& a;
      int& b;
    
      Foo(int& a_)
        : a(a_), b(*(new int(0)))
      {
      }
    
      ~Foo( )
      {
        delete &a; // oopsie
        delete &b;
      }
    };

  9. #24
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointers--when to use them

    No argument here.

    There's rarely any good reason to do anything with the result of a new except assign it to a smart pointer of some type immediately.

  10. #25
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: pointers--when to use them

    The example was just to show that the generalized statement made as in:
    Quote Originally Posted by nuzzle
    - to refer to memory allocated on the heap.
    was not completely true. This was just to show that a reference could be linked to an object on the heap until anytime upto the end of the object's lifetime. Return of new was not what the example was just limited to illustrate. Plus I think you missed the rest of the statements I made there in where I mentioned about possibility of null references and the way you could have nothrow version of new be able to cope up with the syntax and hence leading to a possibility that new could well have been returning a reference. But I guess if you can skip all that context, it might as well sound like a Hopeless Nerd. Thanks for the comment.
    Last edited by exterminator; May 29th, 2009 at 12:00 AM.

  11. #26
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointers--when to use them

    It's my view that it's perfectly permissible to refer to a heap object by reference anywhere except at the level of allocation/deallocation (which should, of course, be the same). That level knows it's heap-allocated, and should carry it in a pointer and deal with it as such. Every other level should make no assumptions about whether it's heap-allocated or not.....except, possibly, when referring to the object via an abstract base class. In that case it's permissible to use either a pointer or reference, whichever is more convenient for the particular usage.

  12. #27
    Join Date
    May 2009
    Posts
    2,413

    Re: pointers--when to use them

    Quote Originally Posted by exterminator View Post
    The example was just to show that the generalized statement made as in:
    was not completely true. This was just to show that a reference could be linked to an object on the heap until anytime upto the end of the object's lifetime. Return of new was not what the example was just limited to illustrate. Plus I think you missed the rest of the statements I made there in where I mentioned about possibility of null references and the way you could have nothrow version of new be able to cope up with the syntax and hence leading to a possibility that new could well have been returning a reference. But I guess if you can skip all that context, it might as well sound like a Hopeless Nerd. Thanks for the comment.

    OP: Are pointers really necessary in C++?

    Exterminator: No, no, no, you can use references instead.

    OP: But are references really necessary then?

    Exterminator: No, no, no, you can use pointers instead.
    Last edited by nuzzle; June 1st, 2009 at 06:07 PM.

  13. #28
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointers--when to use them

    Well, that's more or less true. Each has specific niches where it's slightly more applicable, but the two are pretty interchangeable, all things considered.

  14. #29
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: pointers--when to use them

    Quote Originally Posted by Lindley View Post
    Well, that's more or less true. Each has specific niches where it's slightly more applicable, but the two are pretty interchangeable, all things considered.
    Except, of course, for pointer arithmetic. Sorry to be so obnoxious
    Old Unix programmers never die, they just mv to /dev/null

  15. #30
    Join Date
    May 2009
    Posts
    2,413

    Re: pointers--when to use them

    Quote Originally Posted by exterminator View Post
    The example was just to show that the generalized statement made as in:
    was not completely true. This was just to show that a reference could be linked to an object on the heap until anytime upto the end of the object's lifetime. Return of new was not what the example was just limited to illustrate. Plus I think you missed the rest of the statements I made there in where I mentioned about possibility of null references and the way you could have nothrow version of new be able to cope up with the syntax and hence leading to a possibility that new could well have been returning a reference. But I guess if you can skip all that context, it might as well sound like a Hopeless Nerd. Thanks for the comment.
    So you don't think that one of the most important uses of pointers in C++ is to point to allocated memory on the heap?

    Then why is every known way to get at memory on the heap returning a pointer?

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured