Metropoli BBS
VIEWER: bug.cpp MODE: TEXT (ASCII)
/*
* file: bug.c
*
* description:
*	This file demonstrates a nasty BC++ 3.0 initialization bug.
*	Just compile it with the provided project file in the BC IDE.
*	It appears to be immune to any optimizations.
*	( Be sure to edit the (O)ptions/(D)irectories first! )
*
* By: Gary E. Miller
*	4/28/92
*	gem@cup.portal.com
*	(415)964-1186
*/

#include <stdio.h>

struct ST {
	long stl;
	ST(long a) {stl = a;};
};

long stuff()
{
	// st should be initialized ONCE and ONLY ONCE!
	// instead it is initialized every 0x10000 times
	// that this function is called !!!
	static ST st(0);

	// should return 0 to LONG_MAX and then wrap to LONG_MIN
	// instead it returns 0 to UINT_MAX and then wraps to 0
	// because the constuctor is called a second time at UINT_MAX!!!

	// if you want to know why, just disassemble this function!!
	return(st.stl++);
}

int main(int argc, char **argv)
{
	for ( long l = 0 ; l < 100000 ; l++ ) {
		long m = stuff();
		if ( m != l ) {
			printf("BUG!! stuff()=%ld and l=%ld !\n", m ,l);
			return(1);
		}
	}
	return(0);
}
[ RETURN TO DIRECTORY ]