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

* Re: like to know why it is segmentation fault on simple throw-exception program
  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
       [not found] ` <94ns1jF12lU13@mid.individual.net>
  2 siblings, 0 replies; 6+ messages in thread
From: Jonathan Wakely @ 2011-06-01 22:15 UTC (permalink / raw)
  To: eric; +Cc: gcc-help

On 1 June 2011 23:01, eric wrote:
> Dear comp.lang.c++ reader or advced c++ programers:

This is not comp.lang.c++ and this is not a C++ forum

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

* Re: like to know why it is segmentation fault on simple throw-exception program
  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>
  2 siblings, 1 reply; 6+ messages in thread
From: Jonathan Wakely @ 2011-06-01 22:16 UTC (permalink / raw)
  To: eric; +Cc: gcc-help

On 1 June 2011 23:01, eric wrote:
> int main()
> {
>    try {
>       push_a_lot();
>    }
>    catch (bound_err& err) {
>      std::cerr << "Error: Bounds exceeded\n";
>      std::cerr << "Reason: " << err.what << '\n';

This should be err.what() not err.what

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

* Re: like to know why it is segmentation fault on simple throw-exception program
  2011-06-01 22:16 ` Jonathan Wakely
@ 2011-06-01 23:56   ` Jonathan Wakely
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Wakely @ 2011-06-01 23:56 UTC (permalink / raw)
  To: eric; +Cc: gcc-help

On 1 June 2011 23:16, Jonathan Wakely wrote:
> On 1 June 2011 23:01, eric wrote:
>> int main()
>> {
>>    try {
>>       push_a_lot();
>>    }
>>    catch (bound_err& err) {
>>      std::cerr << "Error: Bounds exceeded\n";
>>      std::cerr << "Reason: " << err.what << '\n';
>
> This should be err.what() not err.what

Oh sorry, ignore that. After actually reading the code I see your
exception has a what data member, I was assuming it had a what()
member function like std::exception.

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

* Re: like to know why it is segmentation fault on simple throw-exception program
       [not found]       ` <is6fgk$ntt$2@dont-email.me>
@ 2011-06-02  3:07         ` eric
  2011-06-02  8:00           ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: eric @ 2011-06-02  3:07 UTC (permalink / raw)
  Cc: gcc-help

On Jun 1, 3:46 pm, Victor Bazarov <v.baza...@comcast.invalid> wrote:
> On 6/1/2011 6:44 PM, Ian Collins wrote:
>
>
>
> > On 06/ 2/11 10:34 AM, Victor Bazarov wrote:
> >> On 6/1/2011 6:16 PM, Ian Collins wrote:
> >>> On 06/ 2/11 10:01 AM, eric wrote:
> >>>> Dear comp.lang.c++ reader or advced c++ programers:
> >>>>  [...]
> >>> Also using exception specifiers is generally regarded as bad practice.
>
> >> Really?
>
> >>> It will land you in all sorts of problems if something you call throws
> >>> some other exception type.
>
> >> That can be too deep for the OP. The inquiry looked very much like a
> >> homework (done by the OP, which is commendable), part of a C++ course,
> >> in which they *might* learn later that an exception specification is
> >> frowned upon by some c.l.c++ inhabitants.
>
> > There are some who don't frown upon them?
>
> <shrug> I don't see them in production code (except the 'throw' with the
> empty parens, which promises that nothing is thrown, as I understand
> it), so I don't care one way or the other.
>
> V
> --
> I do not respond to top-posted replies, please don't ask

---------------------------------------------------------------
Dear Victor or advanced c++ program(especially on g++ camp):
  I follow many of your suggestion, ie change && to || in push
then
after it run
-------------
terminate called after throwing an instance of 'char const*'
Aborted
----------this is better than Segmentation fault, but still not
expected-------

the trouble is happen at call of throw and catch (receiveing) part
it show somehting before throw, but it did not reponse any (or first)
statement in catch block
plz help,(its on gnu/gcc/g++ of linux), thanks a lot in advance, Eric

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

* Re: like to know why it is segmentation fault on simple throw-exception program
  2011-06-02  3:07         ` eric
@ 2011-06-02  8:00           ` Jonathan Wakely
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Wakely @ 2011-06-02  8:00 UTC (permalink / raw)
  To: eric; +Cc: gcc-help

Eric, please stop using this mailing list to learn C++.

If you have questions about G++ you can ask them here, but your
questions are just about basic C++ and are not suitable on this list.

On 2 June 2011 04:07, eric wrote:
> after it run
> -------------
> terminate called after throwing an instance of 'char const*'
> Aborted
> ----------this is better than Segmentation fault, but still not
> expected-------

It's because you throw a char*:

      throw("Push overflows stack");

You probably want to throw a bound_err:

      throw bound_err("Push overflows stack");

But again, please stop using this list to learn C++

If you are posting on comp.lang.c++ then stop repeating the posts here. Thanks.

^ 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).