3

This the part of my OpenGL code, I am getting an error for :

struct Ball {
 float x;
 float y;
 float rot;
 float dir;
 bool rmv;
 Ball* next;
};
Ball* curBall;
void addBall() {
 if (balls==NULL) {
 balls=new Ball;
 balls->next=NULL;
 curBall=balls;
 } else {
 curBall->next=new Ball;
 curBall=curBall->next;
 curBall->next=NULL;
 }
 curBall->x=((float)rand()/(float)(RAND_MAX+1))*(ww-1) +1;
 curBall->y=((float)rand()/(float)(RAND_MAX+1))*(wh-1) +1;
 curBall->dir=((float)rand()/(float)(RAND_MAX+1))*(2*PI-1) +1;
 curBall->rot=((float)rand()/(float)(RAND_MAX+1))*(359) +1;
 curBall->rmv=false;
}

error :
In function ‘void addBall()’:
file.cpp:120: warning: integer overflow in expression
file.cpp:121: warning: integer overflow in expression
file.cpp:122: warning: integer overflow in expression
file.cpp:123: warning: integer overflow in expression
danben
83.8k18 gold badges127 silver badges149 bronze badges
asked May 7, 2010 at 18:01

4 Answers 4

13

Try converting RAND_MAX to a float before adding to it.

curBall->x=((float)rand()/( ((float)RAND_MAX) +1))*(ww-1) +1;

et cetera. RAND_MAX is often equal to INT_MAX, the largest value an integer could hold, thus adding 1 to it while it's still considered an integer pushes it over the integer limit.

answered May 7, 2010 at 18:04
Sign up to request clarification or add additional context in comments.

6 Comments

In addition I would suspect that assuming 32 bit float and int, (static_cast<float>(RAND_MAX) + 1.0f) == static_cast<float>(RAND_MAX) because there won't be enough bits available in the float to represent every integer and the addition will just truncate back down.
I agree that this is the solution, so I'm not going to post my own, but it's worth noting that it appears your two variables dir and rot are in different forms (one in radians, one in degrees). This might cause you some headaches down the road :) been there, done that!
it worked.. now i m facing a small problem.. this is a part of game code.. error was in linux but not in windows.. in windows it was working properly but even after i corrected this error linux is just showing me the window.. the game is not running properly.. :(
In Windows, RAND_MAX is much smaller, I think it's just fifteen bits? 0x7FFF? Modern Unix-flavored operating systems use 32 bits. You could try printf("%d\n", RAND_MAX); to see what it yields on each of the platforms you're targeting.
If RAND_MAX is 32 bits, the addition of 1 will likely not trigger a roll-over to the next greatest value. Remember that 32-bit floating point only has 23 bits of mantissa. You'd need to use doubles.
|
2

It's probably RAND_MAX + 1 that's overflowing, since RAND_MAX may well be == INT_MAX.

answered May 7, 2010 at 18:05

Comments

2

I'd guess your RAND_MAX is equal to your INT_MAX, so all your RAND_MAX+1 pieces are overflowing. Since those are both constants, the compiler can detect it at compile time.

answered May 7, 2010 at 18:05

Comments

2

It could depend on your compiler if RAND_MAX == MAX_INT then RAND_MAX+1 will overflow.

answered May 7, 2010 at 18:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.