Segmentation Fault on stl::resize / stl::clear

Discussion in 'C++' started by Steve, Nov 6, 2007.

  1. Steve

    Steve Guest

    I have segmentation fault when calling resize on an stl vector. I'm
    unsure if I'm doing something horribly wrong or if the stl is being
    dumb. The code breaks down like this:

    -------------------------------------

    class Device {
    public:
    Device() {
    m_isBusy = false;
    m_timeCh = -1.0;
    m_timeService = 0.0;
    } // Device

    ~Device() {}

    private:
    bool m_isBusy;

    double m_timeCh;
    double m_timeService;

    };

    class Queue {
    public:
    Queue() {
    m_totalNumber = 0;
    m_timeCh = 0.0;
    } // Queue

    /**** Destructor ****/
    ~Queue() { clear(); }

    clear();
    private:
    list<Task> theQueue;
    double m_timeCh;
    int m_totalNumber;

    };

    void allocate() {
    CPUS.resize(NUMBER_OF_CPUS); //type vector<device>
    Disks.resize(NUMBER_OF_DISKS); //type vector<device>
    diskQueues.resize(NUMBER_OF_DISKS); //type vector<Queue>
    }

    void clean() {
    CPUS.clear();
    Disks.clear();
    diskQueues.clear();
    Event_Queue.clear();
    }

    -----------------------
    Essentially I repeatedly call allocate then clean. On about the 4th
    time I call allocate I get the wonderful error message:

    Segmentation fault (core dumped)

    Any ideas?
     
    Steve, Nov 6, 2007
    #1
    1. Advertising

  2. Steve

    Steve Guest

    Event_Queue.clear() isnt actually there when the error appears so
    ignore that.
     
    Steve, Nov 6, 2007
    #2
    1. Advertising

  3. Steve

    Andre Kostur Guest

    Steve <> wrote in news:1194316256.702644.202250
    @o3g2000hsb.googlegroups.com:

    > I have segmentation fault when calling resize on an stl vector. I'm
    > unsure if I'm doing something horribly wrong or if the stl is being
    > dumb. The code breaks down like this:


    Given those two choices, I'd bet on you doing something horribly wrong
    (sorry).


    > -------------------------------------
    >
    > class Device {
    > public:
    > Device() {
    > m_isBusy = false;
    > m_timeCh = -1.0;
    > m_timeService = 0.0;
    > } // Device
    >
    > ~Device() {}
    >
    > private:
    > bool m_isBusy;
    >
    > double m_timeCh;
    > double m_timeService;
    >
    > };
    >
    > class Queue {
    > public:
    > Queue() {
    > m_totalNumber = 0;
    > m_timeCh = 0.0;
    > } // Queue
    >
    > /**** Destructor ****/
    > ~Queue() { clear(); }
    >
    > clear();
    > private:
    > list<Task> theQueue;
    > double m_timeCh;
    > int m_totalNumber;
    >
    > };
    >
    > void allocate() {
    > CPUS.resize(NUMBER_OF_CPUS); //type vector<device>
    > Disks.resize(NUMBER_OF_DISKS); //type vector<device>
    > diskQueues.resize(NUMBER_OF_DISKS); //type vector<Queue>
    > }
    >
    > void clean() {
    > CPUS.clear();
    > Disks.clear();
    > diskQueues.clear();
    > Event_Queue.clear();
    > }
    >
    > -----------------------
    > Essentially I repeatedly call allocate then clean. On about the 4th


    Does that mean you do _nothing_ between the allocate and clean?

    > time I call allocate I get the wonderful error message:
    >
    > Segmentation fault (core dumped)


    Not enough code. You haven't supplied the definition of Task (which is
    probably where your real error is). As an example, replace Task with
    int and see if you have the same problem. You also haven't shown what
    Queue::clear() does.

    Oh.. and nitpick: you should get into the habit of using initialization
    lists in your constructors instead of assigning the initial values of
    members.
     
    Andre Kostur, Nov 6, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fudmore
    Replies:
    9
    Views:
    424
    Jonathan Turkanis
    Jan 20, 2004
  2. fudmore
    Replies:
    0
    Views:
    923
    fudmore
    May 10, 2006
  3. mblome
    Replies:
    3
    Views:
    4,758
    Old Wolf
    Jun 14, 2005
  4. Replies:
    8
    Views:
    542
    Clark S. Cox III
    Jul 27, 2005
  5. Bryan
    Replies:
    9
    Views:
    671
    Juha Nieminen
    Nov 1, 2007
Loading...

Share This Page