🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #1544

Strange behavior with const and g++ 1.27

#1544
From: "Douglas C. Schmidt"
Date: Sun, 27 Nov 1988 00:00
78 lines
2831 bytes
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 ) );
}
Technical Details
Message-ID: <8811261600.aa21991@PARIS.ICS.UCI.EDU>
Path: arc1-ca.pugleaf.net!utzoo!telly!ddsw1!mcdchg!rutgers!tut.cis.ohio-state.edu!ORION.CF.UCI.EDU!schmidt%blanche.ics.uci.edu

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!telly!ddsw1!mcdchg!rutgers!tut.cis.ohio-state.edu!ORION.CF.UCI.EDU!schmidt%blanche.ics.uci.edu
From: schmidt%blanche.ics.uci.edu@ORION.CF.UCI.EDU ("Douglas C. Schmidt")
Newsgroups: gnu.g++.bug
Subject: Strange behavior with const and g++ 1.27
Message-ID: <8811261600.aa21991@PARIS.ICS.UCI.EDU>
Date: 27 Nov 88 00:00:29 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 78