Another nonsensical C++ example question ( I hope)

Started by
4 comments, last by mrmrcoleman 18 years, 3 months ago
Hello, I was looking through Sams Teach yourself C++ in 21 days in the section about exception handling and spotted this example. The idea is to find the bug in the code.

class xOutOfMemory
{
	public:
		xOutOfMemory(const String& where) : location(where){}
		~xOutOfMemory(){}
		virtual String where(){return location};

	private:
		String location;
}

int main()
{
	try
	{
		char *var = new char;
		if(var == 0)
			throw xOutOfMemory();
	}
	catch(xOutOfMemory& theException)
	{
		cout << “Out of memory at “ << theException.location() << “\n";
	}
}


Apparently the answer is that the example tries to allocate more memory to report the error, when it is reporting a lack of memory in the first place, but I have 4 problems with this example and I was wondering if somebody could confirm them for me. 1. In the line 'throw xOutOfMemory();' nothing is passed to the constructor. 2. In the output line a method called location is called, this should be where() should it not? 3. As a follow on from point 1, how in the name of holy blue crap, is any sort of output to be expected when no input was provided in the constructor? 4. The original memory problem came from an attempt to allocate memory on the heap, surely the call to 'throw xOutOfMemory();' is an attempt to create an xOutOfMemory object on the stack, and therefore wouldn't this probably be fine? I look forward to hearing your answers/feedback on this, because either I am completely wrong on this (which is very possible and a frequent occurence) or Sam's need to slap themselves around the face. Mark
Advertisement
char *var = new char;if(var == 0){   //some code that will never be executed... =)}


when you fix up the errors you mention, there is something more important.

you code will never be used. ever.

"new" throws an exception all on its lonesome, and as you dont catch that exception in main, your program will crash...
Even worse! Unbelievable.

They should rename the book, Sam's Become A Completely Confused and Useless C++ Programmer in about 45 minutes.

Mark
5. new does not return zero. It throws an exception on failure. The only way to get return-zero-on-failure is to use the nothrow form of new or a braindead/obsolete compiler/settings (i.e. VC6).

Your first three points are all absolutely correct. Your fourth is not since the String member object of xOutOfMemory will almost certainly try to allocate dynamic memory.

If this is a recent printing then:
Quote:Sam's need to slap themselves around the face


Enigma
Quote:Original post by mrmrcoleman
They should rename the book, Sam's Become A Completely Confused and Useless C++ Programmer in about 45 minutes Sam's Novelty Kindling.


Fixed.

Quote:4. The original memory problem came from an attempt to allocate memory on the heap, surely the call to 'throw xOutOfMemory();' is an attempt to create an xOutOfMemory object on the stack, and therefore wouldn't this probably be fine?


Yes, except for the String object. If it's anything like std::string, it "owns" the variable-length data, which will be allocated on the heap, even if the owner object (the String class in this instance) is on the stack.
The book was printed in 2001, which is recent enough for it to fall into the category of kindling I believe.

Thanks, for correcting me on the string aspect of the code and the correct 'new' error procedure. Duly noted. I am trying to revise for interviews and instead I end up spending all my time writing scalding reviews on amazon, something that I urge you are to do also.

May Sam's never inflict anyone else with this inhumane teaching style.

Mark

This topic is closed to new replies.

Advertisement