Thread: destroyed reference variable

  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
    Ireland
    Posts
    8,446
    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 10:04 AM.
    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,366
    >> 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
    Ireland
    Posts
    8,446
    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.
    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, 03: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, 03:48 AM
  4. problem with the library
    By kris.c in forum C Programming
    Replies: 21
    Last Post: 07-10-2006, 08:29 PM
  5. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM