Strange std::vector behaviour

H

Hamish

Hello, lately I've been having a lot of trouble with the std::vector. Seems
to create unpredictable behaviour within my code.

Example:

struct Switch{
int i;
int j;
double d;
};

std::vector<Switch> Switches;

Now I have a for...loop:

int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}

The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.

However, if I use the line:
for(int i=0;i<h-1;i++){
then there is not a problem.

Anyone got any ideas as to what is going on, cos this is ruining my New
Year!
 
C

Cy Edmunds

Hamish said:
Hello, lately I've been having a lot of trouble with the std::vector.
Seems
to create unpredictable behaviour within my code.

Example:

struct Switch{
int i;
int j;
double d;
};

std::vector<Switch> Switches;

Now I have a for...loop:

int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}

The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.

However, if I use the line:
for(int i=0;i<h-1;i++){
then there is not a problem.

Anyone got any ideas as to what is going on, cos this is ruining my New
Year!


std::vector<T>::size() returns an unsigned type. Hence 0 - 1 evaluates to
something like 0xffffffff
 
C

Chris

Cy Edmunds said:
std::vector<T>::size() returns an unsigned type. Hence 0 - 1 evaluates to
something like 0xffffffff

or, you could use iterators:
typedef std::vector< Switch > tSwitchContainer
typedef tSwitchContainer::iterator tSwitchItr;

tSwitchContainer Switches;

for ( tSwitchItr cur = Switches.begin(); cur != Switches.end(); cur++)
{
// do stuff.....
}

nice thing about this is you could use any container and the code would
still work

-c
 
I

Ioannis Vranos

Hamish said:
Hello, lately I've been having a lot of trouble with the std::vector. Seems
to create unpredictable behaviour within my code.

Example:

struct Switch{
int i;
int j;
double d;
};

std::vector<Switch> Switches;

Now I have a for...loop:

int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}

The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.

However, if I use the line:
for(int i=0;i<h-1;i++){
then there is not a problem.

Anyone got any ideas as to what is going on, cos this is ruining my New
Year!


Switches.size() returns a vector<Switch>::size_type which is an unsigned
integer type.


Thus vector<Switch>::size_type(-1) is the maximum value of that type.


Your code can be fixed like this:


for(int i=0; i<Switches.size(); ++i)
/*do stuff here*/


or better:


for(vector<Switch>::size_type i=0; i<Switches.size(); ++i)
/*do stuff here*/
 
M

Mike Wahler

Hamish said:
Hello, lately I've been having a lot of trouble with the std::vector. Seems
to create unpredictable behaviour within my code.

Example:

struct Switch{
int i;
int j;
double d;
};

std::vector<Switch> Switches;

Now I have a for...loop:

int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}

The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.

for(std::vector<Switch>::size_type i = 0; i < Switches.size(); ++i)
/* etc */

If for some reason you really want to ignore the last
element (if one exists):

if(!Switches.empty())
However, if I use the line:
for(int i=0;i<h-1;i++){
then there is not a problem.

If 'h' starts at zero, the behavior is undefined.
Anyone got any ideas as to what is going on, cos this is ruining my New
Year!

Vector indices begin with zero and run through 'size()' - 1

-Mike
 
I

Ioannis Vranos

Hamish said:
Hello, lately I've been having a lot of trouble with the std::vector. Seems
to create unpredictable behaviour within my code.

Example:

struct Switch{
int i;
int j;
double d;
};

std::vector<Switch> Switches;

Now I have a for...loop:

int h = Switches.size();
for(int i=0;i<Switches.size()-1;i++){
/*do stuff here*/
}

The problem is, when h (Switches.size()) equals 0, the for...loop is still
entered, and runs infinitely.

However, if I use the line:
for(int i=0;i<h-1;i++){
then there is not a problem.

Anyone got any ideas as to what is going on, cos this is ruining my New
Year!



Switches.size() returns a vector<Switch>::size_type which is an unsigned
integer type.


Thus vector<Switch>::size_type(-1) is the maximum value of that type.


Your code can be fixed like this:


for(int i=0; i<Switches.size(); ++i)
/*do stuff here*/


or better:


for(vector<Switch>::size_type i=0; i<Switches.size(); ++i)
/*do stuff here*/
 
E

E. Mark Ping

If for some reason you really want to ignore the last
element (if one exists):

if(!Switches.empty())
for(std::vector<Switch>::size_type i = 0; i < Switches.size() - 1; ++i)
/* etc */

Which of course is a big problem if the size is 0. When dealing with
unsigned types, I find avoiding subtraction to be much safer.

for(int i = 0; i+1 < Switches.size(); ++i)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top