Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #6047
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: like to know why it is segmentation fault on simple throw-exception program |
| Date | 2011-06-01 18:44 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <is6fc7$ntt$1@dont-email.me> (permalink) |
| References | <fca61986-de7e-40b8-80e3-0a40633a3bf1@j13g2000pro.googlegroups.com> |
On 6/1/2011 6:01 PM, eric wrote:
> Dear comp.lang.c++ reader or advced c++ programers:
>
> I copied a piece of code from page 397 of book (Practical C++
> programming), example22-1, stack_e1.cpp
> about Throwing an Exception.
> after a little modification, it successfully compile on my gnu/g++/
> ubuntuLinux system
> but when i run it, it response
> Segmentation fault
> -------------------------------------------this is the
> program--------------------------------------------------------------
> /**************************************************
> * stack *
> * A file implementing a simple stack class *
> **************************************************/
> #include<cstdlib>
> // #include<string>
> #include<iostream>
> #include<assert.h>
Why do you need 'assert.h'?
>
> const int STACK_SIZE = 100; // Maximum size of a stack
>
> /*****************************************************
> * bound_err -- a class used to handle out of bounds *
> * execeptions. *
> *****************************************************/
> class bound_err {
> public:
> const std::string what; // What caused the error
>
> // Initialize the bound error with a message
> bound_err(const std::string& i_what) : what(i_what) {};
Format nit-pick: drop the trailing semicolon.
> // Assignment operator defaults
> // bound_err(bound_err) -- default copy constructor
> // ~ bound_err -- default destructor
> };
>
> /*******************************************************
> * Stack class *
> * *
> * Member functions *
> * init -- initialize the stack. *
> * push -- put an item on the stack. *
> * pop -- remove an item from the stack *
> *******************************************************/
> // The stack itself
> class stack {
> private:
> int count; // Number of items in teh stack
> int data[STACK_SIZE]; // The items themselves
> public:
> // Initialize the stack
> stack(): count(0) {};
Format nit-pick: drop the trailing semicolon.
> // Copy constructor defaults
> // Assignment operator defaults
>
> // Push an item on teh stack
> void push(const int item) throw(bound_err);
>
> // Pop an item from the stack
> int pop() throw(bound_err);
> };
> /
> **********************************************************************
> * stack::push -- push an item on the stack
> *
> *
> *
> * Parameters
> *
> * item -- item to put in the stack
> *
>
> **********************************************************************/
> inline void stack::push(const int item) throw(bound_err)
> {
> if ((count< 0)&&
> (count>= sizeof(data)/sizeof(data[0]))) {
Check the condition. Can 'count' be less than 0 AND greater than the
size of the array *simultaneously*?
> throw("Push overflows stack");
Are you sure this throw will actually throw a 'bound_err' as your
specification promises? Put a breakpoint in the c-tor for 'bound_err'
and see whether it gets hit. I suspect that the compiler may not be
able to understand two user conversions in a row (char[] -> string,
string -> bound_err) and throws a pointer to char.
> }
> data[count] = item;
> ++count;
> }
> /*********************************************************************
> * stack::pop -- get an item off the stack. *
> * *
> * Returns *
> * The top item fromt the stack. *
>
> *********************************************************************/
> inline int stack::pop() throw(bound_err)
> {
> // Stack goes down by one
> --count;
>
> if ((count< 0)&&
> (count>= sizeof(data)/sizeof(data[0]))) {
Check the condition. Can 'count' be less than 0 AND greater than the
size of the array *simultaneously*?
> throw("Pop underflows stack");
Same note as before, but also, how can 'count' be greater than the size
of the 'data' array here?
> }
> // Then we return the top value
> return (data[count]);
> }
> static stack test_stack; // Define a stack for our bounds
> checking
> /
> ***************************************************************************
> * push_a_lot -- Push too much on to the
> stack *
>
> ***************************************************************************/
> static void push_a_lot() {
> int i; // Push counter
>
> for (i=0; i< 5000; i++) {
Get used to the C++ idiom of declaring/defining the loop counter inside
the parentheses:
for (int i = 0; i < 5000; i)) ) {
> test_stack.push(i);
> }
> }
>
> int main()
> {
> try {
> push_a_lot();
> }
> catch (bound_err& err) {
> std::cerr<< "Error: Bounds exceeded\n";
> std::cerr<< "Reason: "<< err.what<< '\n';
> exit (8);
> }
> catch (...) {
> std::cerr<< "Error: Unexpected exception occurred\n";
> exit(8);
> }
> return (0);
> }
>
V
--
I do not respond to top-posted replies, please don't ask
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
like to know why it is segmentation fault on simple throw-exception program eric <cneric12lin0@gmail.com> - 2011-06-01 15:01 -0700
Re: like to know why it is segmentation fault on simple throw-exception program Ian Collins <ian-news@hotmail.com> - 2011-06-02 10:16 +1200
Re: like to know why it is segmentation fault on simple throw-exception program Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-01 18:34 -0400
Re: like to know why it is segmentation fault on simple throw-exception program Ian Collins <ian-news@hotmail.com> - 2011-06-02 10:44 +1200
Re: like to know why it is segmentation fault on simple throw-exception program Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-01 18:46 -0400
Re: like to know why it is segmentation fault on simple throw-exception program eric <cneric12lin0@gmail.com> - 2011-06-01 20:07 -0700
Re: like to know why it is segmentation fault on simple throw-exception program Paavo Helde <myfirstname@osa.pri.ee> - 2011-06-02 00:28 -0500
Re: like to know why it is segmentation fault on simple throw-exception program Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-02 07:59 -0400
Re: like to know why it is segmentation fault on simple throw-exception program red floyd <redfloyd@gmail.com> - 2011-06-02 11:20 -0700
Re: like to know why it is segmentation fault on simple throw-exception program Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-01 18:44 -0400
csiph-web