public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* like to know why it is segmentation fault on simple throw-exception program
@ 2011-06-01 22:01 eric
  2011-06-01 22:15 ` Jonathan Wakely
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: eric @ 2011-06-01 22:01 UTC (permalink / raw)
  Cc: gcc-help

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>

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) {};
    // 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) {};
        // 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]))) {
       throw("Push overflows stack");
    }
    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]))) {
     throw("Pop underflows stack");
  }
  // 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++) {
     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);
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-06-02  8:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-01 22:01 like to know why it is segmentation fault on simple throw-exception program eric
2011-06-01 22:15 ` Jonathan Wakely
2011-06-01 22:16 ` Jonathan Wakely
2011-06-01 23:56   ` Jonathan Wakely
     [not found] ` <94ns1jF12lU13@mid.individual.net>
     [not found]   ` <is6epo$kki$1@dont-email.me>
     [not found]     ` <94ntlmF12lU14@mid.individual.net>
     [not found]       ` <is6fgk$ntt$2@dont-email.me>
2011-06-02  3:07         ` eric
2011-06-02  8:00           ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).