growing numbers at runtime like ofGetElapsedTimeMillis

Hi

I was wondering what happens to an ofApp that is running for an infinite time and uses functions like ofGetElapsedTimeMillis() or ofGetFrameNum()? They all return an increasing integer and at some point wouldn’t they be too big to handle? It would probably take a long time. But still, what would happen in this case? Would the app crash or behave unexpectedly?

lg.ph

when you arrive to the limit of an integer it goes back to the negative limit, if it’s an unsigned integer to 0. so if you are comparing the previous and the current value you’ll get a huge difference which will break whatever you are controlling with it like an animation.

You can compare if the previous value is greater than the current one, and calculate the difference around the limits

that’s the implementation. (in ofUtils.cpp)

  
int ofGetElapsedTimeMillis(){  
	return (int)(ofGetSystemTime() - startTime);  
}  

the cast to int will just wrap around if the castee (which is unsigned long) is too large. when that happens depends on your compiler and OS. sizeof(int) will show you.

this from ofGetSystemTime():

  
// this is from freeglut, and used internally:  
/* Platform-dependent time in milliseconds, as an unsigned 32-bit integer.  
 * This value wraps every 49.7 days, but integer overflows cancel  
 * when subtracting an initial start time, unless the total time exceeds  
 * 32-bit, where the GLUT API return value is also overflowed.  
 */  

1 Like