new and delete for references

This is a discussion on new and delete for references within the C++ Programming forums, part of the General Programming Boards category; I know it's more of a personal preference thing, but I really don't like to use pointers, and prefer to ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,402

    new and delete for references

    I know it's more of a personal preference thing, but I really don't like to use pointers, and prefer to use references as much as possible. I was wondering if there is any plan to add operators to the C++ standard that function like new and delete, but create and destroy references instead of pointers.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    "reference" is a reference to a pointer. It is not a pointer.

    Also, the personal preference this works on small projects. Some large projects will require the use of dynamic memory. If you don't like pointers then look into boost::weak_ptr and boost::shared_ptr.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    Your preference seems to be a purely syntactical one. I doubt they will do anything like you suggest. The best course, probably, is to learn why references aren't the same as pointers and understand what pointers bring in terms of language features.

    References are merely aliases for another object; it's likely that they aren't even involved in memory management behind the scenes as pointers are.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm guessing you're used to Java, which uses the word "reference" when what they really mean is garbage collected pointer.
    A reference has no memory, it's just another name for some other variable, and you cannot re-assign a reference after it's initialized. That's the main point of references -- once it's set, you don't have to worry about it getting set to some other variable.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There are absolutely no plans to do so, and I don't think anyone even considered sending in a proposal. It would be completely redundant. Pointers (and smart pointers) are perfectly sufficient for dynamic allocation.
    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

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,402
    I don't think you understand what I mean.

    I used the word 'add' not 'replace' regarding the features I suggest. I understand that pointers are critical to c++, and I do use them where it's necessary, and where it makes for better code. However, it might also be nice to create a new instance of a class as a reference, and then be able to delete that object later. The simple truth of the pointers vs. references issue is that in most cases - dynamic memory management being one exception, references provide equivalent functionality to pointers, and permit a significant reduction in keystrokes for large projects, as you can use . instead of ->, and when you consider how many classes and structs you might use in a large project, it only makes sense that any reduction in keystrokes will be helpful.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Elkvis View Post
    I don't think you understand what I mean.

    I used the word 'add' not 'replace' regarding the features I suggest. I understand that pointers are critical to c++, and I do use them where it's necessary, and where it makes for better code. However, it might also be nice to create a new instance of a class as a reference, and then be able to delete that object later. The simple truth of the pointers vs. references issue is that in most cases - dynamic memory management being one exception, references provide equivalent functionality to pointers, and permit a significant reduction in keystrokes for large projects, as you can use . instead of ->, and when you consider how many classes and structs you might use in a large project, it only makes sense that any reduction in keystrokes will be helpful.
    But wouldn't that completely change (destroy might be a more appropriate term) the entire meaning of a reference in C++? A reference is simply a nickname for an (already-existing) object, which is guaranteed to always and forever be stuck to the same object. Using new and delete is not really consistent with that "one object" principle, in my view.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can always write
    Code:
    obj& o = *new obj;
    and later
    Code:
    delete &o;
    That doesn't change the fact that it's a bad idea.

    1) You can't assign NULL to a reference after deleting the object.
    2) References do not signal ownership. This is more of a cultural thing, but it is important nevertheless.
    3) A technical reason for #2 is that you cannot take ownership if you have a reference, since a) you can't set the origin to NULL and b) the reference would have to be newly created.

    In the end, though, #2 is the most compelling reason not to do it. References means "yeah, I'm using that object, but I'm not responsible for cleaning it up". (You are responsible for knowing when the object dies so that you don't access a zombie, of course.)

    It's just the way we are trained in C++.
    Reference: no ownership, full responsibility for lifetime understanding.
    shared_ptr: shared ownership, responsible only for avoiding circles.
    auto_ptr/unique_ptr: exclusive ownership, transferable.
    scoped_ptr: exclusive ownership, non-transferable.
    weak_ptr: no ownership, gets informed about lifetime.

    This training, and the experience to recognize what is needed, is what greatly reduces the pain of memory management in C++.


    There's more. Since you cannot overload the . operator, you cannot write "smart references" the same way you write smart pointers. To have automated memory management (and you really don't want non-automated memory management), you'd need the wrappers to have pointer syntax anyway, giving you nothing.


    The argument that . instead of -> significantly reduces the amount of code is bogus anyway. Let's assume that your variable names have an average of 4 characters (quite short, but there you are), while your functions have more descriptive names with an average of 10 characters. Then any given qualified member function access through a pointer has 16 characters, through a reference 15. That's a saving of 6.25%, but only for qualified member function accesses, discounting the argument list, assignment of the return value, and of course all the expressions (the majority) that are not qualified member function accesses. In the end, you'll get a saving in the tenths or even hundredths of a percent.
    And besides that, on a German keyboard at least - is a right-handed key and > a left-handed key; thus the time needed to type -> is only marginally longer than that needed 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

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    I think the issue is well covered but I came up with yet another rebuttal.

    The new purpose you would give references Elkvis involves a new data-type incredibly similar to pointers (called foo for the purpose of this post), except that, if we're going for a zero added keystroke approach, you could not access the address of the foo variable. This means that if you ever needed to refer to a foo, you couldn't (or the programmer would need to infer the context) because the lack of a dereference operator makes your code's intent ambiguous.

    Example
    Code:
    Mytype foo bar = new Mytype;
    Mytype quz;
    bar.f();
    quz.f();
    
    bar = quz; //is this a memory leak 
               //or a harmless assignment of the referred type?
    
    delete bar;
    If you added the ability to refer to or assign foos you essentially have a pointer.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What you could do is make some sort of reference counting container. But at that point people will start asking why not just use boost.

    Example:
    Code:
    template <typename T>
    class obj
    {
    private:
      int ref;
    
    protected:
      virtual void destroy()
      {
        if(!--ref)
          delete this;
    
        if(ref < 0)
          ref = 0;
      }
      virtual ~obj() { }
    
    public:
      obj() : ref(1) { }
      obj(const obj &o) : ref(o.ref + 1) { }
      void add() { ++refs; }
      void del() { destroy(); }
    }

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elkvis View Post
    references provide equivalent functionality to pointers, and permit a significant reduction in keystrokes for large projects, as you can use . instead of ->, and when you consider how many classes and structs you might use in a large project, it only makes sense that any reduction in keystrokes will be helpful.
    OMG! How incredibly lazy do you have to be that you can't type 1 extra character???
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To be honest, strictly from a production perspective. It is faaaaaaaaaar more productive to clearly show where pointers are used than it is to unclearly mar your work with references that no one really knows what to do with.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    Clearly we should do a time-motion study on this issue then.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Meanwhile, Elkvis is not invited to be on any of my development teams like EVER. I bet you this could pretty easily be proven to be ineffective if we made two development teams of three. Allow no communication between team members other than CVS, and then time their efforts to make a program with identical specifications.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I really don't like to use pointers, and prefer to use references as much as possible. <<

    I would focus on using smart pointers. They are a much better idea than raw pointers anyway, and the syntax is slightly nicer.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21