This is why I don't go outside.
Shannon and I ventured out for about 20 minutes today to get various houeshold products and to ship my CyberPower UPS out for warranty repair at, coincidentally, UPS. The shipping went smoothly so we walked next door to Bartell Drugs for the household products. While we were shopping I overheard a conversation between a pharmacist or technician and a customer that went something like this:
Pharmacist: "Oh, well, I can't sell you the ones that fix that. You'll need a prescription."
Customer: "Really? I see."
Pharmacist points to the box in the customer's hand.
Pharmacist: "You can try those. It's a homeopathic remedy. It might help a little."
At this point - and this is very out of character - I interjected. Laughing, I said "Excuse me" and told the customer that he most certainly should not purchase anything homeopathic. The pharmacist looked at me a little taken aback while I rolled my eyes and walked away.
It's outrageous to me that someone who works at a drug store would sell a consumer something that is less effective even than snake oil because she was unable to make a sale on the prescription drug. If I was a little more assertive, I would have had a discussion with the store manager. In any case, I won't be shopping at Bartell Drugs any more and I suggest that other people who understand the ridiculousness and inanity of homeopathic "cures" don't either.
Harumph. I becoming more and more like an old man every day.
I got asked this question as an interview question at the Engineering Expo at U of I and thought it was a really good interview question because it's non-trivial but a reasonable answer can be given if you just think about it for a few minutes. The humorous thing was that once the interviewer and I started talking about it, it turned out he didn't really know the answer himself - it was more intended to see how an interviewer would respond to an odd question. So what would happen?
Caveat: this is probably an incomplete answer so please feel free to correct me or add things I'm missing.
The first step is to ask "What does delete p do?" It's basically a two step operation:
- Call p's destructor (your code)
- Free the allocated memory for p (C++ runtime)
delete this works in exactly the same way. The first thing that happens is the destructor for your class is called - so there goes all your heap allocated memory (you don't have any memory leaks, right?). No big deal, this just means that after calling delete this you can't use any of your heap allocated memory or anything else that you clean up in your destructor.
Next, the C++ runtime frees the memory it allocated for your class. This means bye-bye for your member variables and function pointers. Interestingly, they may continue to work for a little while since their memory might not be reclaimed right away. I remember reading on Raymond Chen's blog that the Windows folks had to tweak the Windows memory manager to delay reclaiming memory for certain programs because those programs relied on just this behavior. Remember that old saying "DOS ain't done until Lotus won't run"? Yeah, it's bull. In any case, this means that you can't call any member functions or access any member variables after you call delete this.
OK, so where does that leave us? Well, delete this hasn't messed with our instruction pointer (except to make the proper function calls), so your code will happily continue to run. As long as you play it safe and pretend that your class instance basically doesn't exist any more you can execute as much code as you want. This might even be handy because you now have the ability to safely commit class suicide. However, just like human suicide, it would probably be nice to leave a note for your caller in the form of a return value to let them know what happened.
Add a Comment
The foam on my latté is more appealing to me than the espresso.
Kill me now.
But seriously, the foam is the best part. Mmmmmmmm...
Add a Comment
I've always enjoyed logic (even if I haven't been very logical my whole life) and I predict that one of my regrets in life will be dropping my Logic and Arguement class in my final semester of school so that I could concentrate on my Computer Engineering classes. I actually ended up keeping the books because I found them so interesting.
Lately, it's become fashionable to point out logical fallacies in your opponent's arguement and, even if they don't necessarily counter his claims, the accusation might make him stumble for a second or confuse him into conceding a point. "Let's not devolve this into an ad hominem" is a typical line. Often, people on the losing side of an argument will search for logical fallacies in order to try to dismiss the other person's claims. This can be quite irritating, as the logical fallacies are often misapplied. Some, however, are very useful in countering claims by an opponent that you don't readily have evidence to defend against.
My favorite logical fallacy is "false compromise." This is where two opponents take polarly opposite stances on an issue. Let's say the opponents are a creationist who says that God created everything on Earth 6,000 years ago and a geologist who claims that the Earth is actually hundreds of millions of years old and that life evolved. The two bicker and banter and present their sides. Unfortunately for the creationist, the geologist has a lot more evidence on his side and is swaying the audience. So what does the creationist do? He commits the "false compromise" fallacy:
"Well, you claim the Earth is millions of years old and that life evolved on the Earth. I claim the Earth is very young and that God created life from thin air. In all honesty, the truth is probably somewhere in between. For example, there may have been some evolution, but God guided it."
The audience likes this. They are used to hearing that the truth lies between two extremes, especially when it comes to politics. But the truth isn't always between two extremes. Sometimes, the truth is at one extreme. If I claim that I wrote this in the United States and you claim that I wrote it in Nepal, the truth is certainly not that I probably wrote it in the Pacific Ocean.
This is how we got intelligent design.
Love, Mom *(big surprise, right?)*
Add a Comment
Last Thursday afternoon, Steven Sinofsky treated the entire Windows division (which includes me) to go to a special screening of JJ Abrams's new Star Trek. We got free popcorn, candy, and ginormous drinks. The best part was that the screening was actually 14 hours before the premier at midnight, so the only people who got to see Star Trek before us were the critics. There are certain perks to working for The Evil Empire. :-)
Overall, although I'm not a huge Star Trek geek, I thought the movie was really solid. I was fully expecting Abrams to go off-the-wall crazy with the plot and special effects, but I was pleasantly surprised that the plot was sane and the acting was actually quite good. The twists were believable and the story came together in the end. Maybe this summer I'll go through the Star Trek canon; I could use another geeky series.
Add a Comment