Snowman
Snowman

Reputation: 32091

Random numbers causing problems

Say I have a member variable vector<bool> rightWall;. rightWall has width*height indices. I want to access a random index of rightWall. So I do:

index1=rand()%(width*height-1);
rightWall[index1]=true;

But I get Valgrind errors: Invalid read of size 8. rightWall was never initialized or anything, and I'm not sure it's really supposed to, since its only bools. What can be the problem?

Upvotes: 0

Views: 90

Answers (1)

shoosh
shoosh

Reputation: 79021

To get a vector of a certain size you need to initialize it like so:

vector<bool> rightWall(width*height);

or resize it:

vector<bool> rightWall;
rightWall.resize(width*height);

Upvotes: 5

Related Questions