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

S

Steve

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?
 
A

Andre Kostur

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();
}

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,105
Latest member
sheetaldubay7750ync
Top