A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

http://ideone.com/zBPzc why was "Goodbye, World!" only printed once?

  • This Question is Closed
  1. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://ideone.com/REaCY here it's printed twice!

  2. asnaseer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Looks like a race condition during program termination. In the first case, the program terminated before the output from the destructor of the class Ball had a chance to make it to the output stream.

  3. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    right; taking away the delete gives me this: http://ideone.com/VHDOT

  4. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    what if I make the balls static objects? I think it will work fine.

  5. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://ideone.com/JzRRO

  6. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    now I get it... it's because I never call ball2's destructor in my original post (both ball1 and ball2 are dynamically allocated)

  7. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    whereas in my last example ( http://ideone.com/JzRRO) as soon as main terminates their destructors are called.

  8. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    called automatically*

  9. asnaseer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    the order in which static objects are deleted is not usually well defined. it also goes against the principals of good programming practices.

  10. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    right.... so how do I make sure, say, the static ball1 is deleted before the static ball2?

  11. asnaseer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    delete ball1; delete ball2;

  12. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    anyway... my example probably breaks 10 different C++ proper practices (using this in class methods, etc.)

  13. asnaseer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    if you are familiar with them, then you may want to consider using the shared pointers provided by the boost library to manage memory for you.

  14. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I'm still in like the first few chapters of my C++ primer book. I'm sure I will eventually learn about proper coding practices (RAII, etc)

  15. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I only have the 4th edition though... so I'm sure there's probably something new that isn't covered :(

  16. asnaseer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    ok - good. I guess you have to be shown the bad practices first in order to fully appreciate the good ones. :)

  17. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I might want to get this new book: http://www.amazon.com/Primer-5th-Stanley-B-Lippman/dp/0321714113 which will release after half a year :-P

  18. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://ideone.com/oRc3z C++ is acting strange now :(

  19. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.