destroyed reference variable

This is a discussion on destroyed reference variable within the C++ Programming forums, part of the General Programming Boards category; assume tst is name of the class after delete ptr how i can destroyed rObj ? thanks Code: tst *ptr ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    5

    destroyed reference variable

    assume tst is name of the class
    after delete ptr how i can destroyed rObj ?
    thanks

    Code:
    	tst *ptr = new tst() ;
    	tst &rObj = *ptr;
    	delete ptr ;

    (I beg your pardon for my english)

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,575
    Well... you do have a problem there don't you?

    rObj will be only destroyed when it goes out of scope. What you should do though is set ptr to null. That will ensure rObj will not yield undefined behavior.

    Code:
    int *ptr = new int(12) ;
    int &rObj = *ptr;
    delete ptr;
    
    std::cout << rObj  // undefined behaviour
    
    ptr = 0;
    
    std::cout << rObj // ok.
    EDIT: I'm not sure but I can bet you are strongly discouraged to create references to dereferenced dynamically allocated objects. And that is exactly the reason why.
    Last edited by Mario F.; 08-13-2006 at 11:04 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> after delete ptr how i can destroyed rObj ?
    You can't "destroy" rObj, you just have to make sure you don't use it anymore.

    >> That will ensure rObj will not yield undefined behavior.
    No, actually it won't. All you can do is make sure you don't use rObj after you delete ptr. What do you expect the output of the second cout to be?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,575
    Yes. I was not sure about it to be honest. Should have included a small note saying so.

    I was expecting the second output to generate a runtime error. Which would always be preferable than undefined behavior. But after your post I tested it, and it doesn't.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    yes,I can't change rObj value after initialization

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 04:15 PM
  2. non-const reference and const reference
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2007, 05:03 AM
  3. A general quaetion on portability
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-20-2006, 04:48 AM
  4. problem with the library
    By kris.c in forum C Programming
    Replies: 21
    Last Post: 07-10-2006, 09:29 PM
  5. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 05:54 PM

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