Thread: memory error

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    memory error

    Hi,

    Currently working through Stroustrup - Programming Principles and having problem with a particular function he advises to use:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void error(const string& s)
    {
    	throw runtime_error(s);
    }
    
    
    
    int main()
    
    {
    	cout << "Please enter an expression (we can handle +, -, / or *): ";
    	int lval = 0;
    	int rval;
    	char op;
    	cin >> lval;
    	if(!cin) error("no first operand");
    
    	while(cin>>op){
    		cin >> rval;
    		if(!cin) error("no second operand");
    		switch(op){
    			case '+':
    				lval+=rval;
    				break;
    			case '-':
    				lval-=rval;
    				break;
    			case '*':
    				lval *=rval;
    				break;
    			case '/':
    				lval /= rval;
    				break;
    			default:
    				cout << "Result: " << lval << '\n';
    				
    		}
    	}
    	error ("bad expression");
    	system("pause");
    				return 0;
    	
    
    
    }
    When I enter bad input to call the error function I get this message: Unhandled exception at 0x761c42eb in Accelerated C++.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0022fb54..

    Anybody any idea why?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That should be expected: the function apparently deliberately does not catch the exception thrown.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Because you're throwing an exception...? Maybe you should try reading up on exceptions and exception handling before continuing?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    I have done a little reading on exception handling and am aware of try, throw, catch etc. However, the code above is taken directly from Stroustrup's book?

    Also when I try to use the calculator with the code above, it doesnt work properly, for example when i type 2+2 and then keep pressing enter nothing happens. Only when I type another integer without and operator does it give me the result!!!! Damn Stroustrup! Anybody any ideas?

    Thanks

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    However, the code above is taken directly from Stroustrup's book
    most likely, you've missed something.

    Damn Stroustrup!
    sacrilege!

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by m37h0d View Post
    most likely, you've missed something.



    sacrilege!
    Ha.....code is definately correct, except for the error function which Stroustrup has included in his own header file. He has used:
    Code:
    inline void error(const string& s)
    {
    	throw runtime_error(s);
    }
    According to Stroustrup that is meant to disguise the throw? The function call is as above in my original code???

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What are you expecting to happen?

    Does he have a small disclaimer at the front of the book, something to the effect of "not all examples show full error handling logic..."?
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Dino View Post
    What are you expecting to happen?

    Does he have a small disclaimer at the front of the book, something to the effect of "not all examples show full error handling logic..."?
    I was expecting it to return an error string. Or at the very least not make the program unworkable.

    I can't see any disclaimer either.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You'll have to handle the exception and print the error string.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    on closer inspection...it appears to be doing exactly what it's intended to. stop freaking out because it's not doing what you expect it to and try to understand what it does.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by m37h0d View Post
    on closer inspection...it appears to be doing exactly what it's intended to. stop freaking out because it's not doing what you expect it to and try to understand what it does.
    Are you saying that when runtime_error is thrown, that's what causes the message box and subsequent error?

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Doing a bit of research it is apparently the way that exceptions are handled by the debugger. A little further research is required I think....thanks for the response so far guys.

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by darren78 View Post
    Are you saying that when runtime_error is thrown, that's what causes the message box and subsequent error?
    yes, exactly.

    also, this:

    Also when I try to use the calculator with the code above, it doesnt work properly, for example when i type 2+2 and then keep pressing enter nothing happens. Only when I type another integer without and operator does it give me the result!!!! Damn Stroustrup! Anybody any ideas?
    doesn't at all match with the apparent usage case.

    study the code; particularly, try to understand the program execution flow.

    like i said...you missed something
    Last edited by m37h0d; 10-26-2009 at 02:19 PM.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by m37h0d View Post
    yes, exactly.

    also, this:



    doesn't at all match with the apparent usage case.

    study the code; particularly, try to understand the program execution flow.

    like i said...you missed something

    Thanks for the responses.....I understand now.

    Cheers.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    (1) That code is not copied exactly: system("pause"); is never used in PPP
    (2) The main() for that example has a try block
    (3) You can find all the code for the book at Stroustrup: Programming -- Principles and Practice Using C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM