[gnu.g++.bug] Strange behavior with const and g++ 1.27

schmidt%blanche.ics.uci.edu@ORION.CF.UCI.EDU ("Douglas C. Schmidt") (11/27/88)

Hi,

  The following program displays very strange behavior...  It appears
that a constant magically changes its value inside a loop.  When the
const qualifier is removed the program runs correctly.  When linked
with the -lcurses and -ltermcap libraries, the output of this program
should print a title followed by 7 double spaced menu options.
However, g++ ( both with and without -O, on the sun 3 and sun 4 )
produces the first 3 options double spaced, and the remainder single
spaced.  If one examines the source code it is obvious that this only
could happen if the constant is changing its value inside the for
loop!  CC 1.2.1 gets it correct, as does g++ if the line:

   const int SPACING = (((option_num*2 + option_row) < 22 )? 2 : 1 );

is changed to

   int SPACING = (((option_num*2 + option_row) < 22 )? 2 : 1 );

Doug

----------------------------------------
typedef unsigned short chtype;
struct _win_st {
	short	_cury, _curx;
	short	_maxy, _maxx;
	short	_begy, _begx;
	short	_flags;
	chtype	_attrs;
	char 	_clear;
	char 	_leave;
	char 	_scroll;
	char 	_use_idl;
	char 	_use_keypad;	 
	char 	_use_meta;	 
	char 	_nodelay;	 
	chtype	**_y;
	short	*_firstch;
	short	*_lastch;
	short	_tmarg,_bmarg;
};
typedef struct _win_st	WINDOW;
extern WINDOW	*stdscr, *curscr;

void Display( char *option[], 
              char *title,
              int  option_num ) {
   const int TITLE_ROW = 1;
   const int TITLE_COL =  36 - ( strlen( title )/2 );
   initscr();
   wclear(stdscr) ;
   wmove(stdscr,  TITLE_ROW,  TITLE_COL ) ;
   printw( "*** %s ***", title );
   int option_col = 40 - strlen(option[0]) / 2;
   int option_row = 3;
// remove the following const and the program works correctly
   const int SPACING = (((option_num*2 + option_row) < 22 )? 2 : 1 );

   if( option_col > TITLE_COL ) {
      option_col = TITLE_COL;
   }
   for( int i = 0; i < option_num; i++ ) {
      wmove(stdscr,  option_row,  option_col ) ;
      printw( "%d. %s %d %d\n", i+1, option[i], option_row, SPACING );   
      option_row += SPACING;
   }   
   wrefresh(stdscr) ;
   endwin();
}

char *option[] = {"Maximum", "Minimum", "Mean", "Median", "Total", 
                  "Get All Value", "Quit"};
main()
{
    char *title    = "STATISTIC MENU";
    Display( option, title, sizeof ( option ) / sizeof ( *option ) );
}