🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by "Douglas C. Schmidt" Sun, 27 Nov 1988 00:00
Strange behavior with const and g++ 1.27
#1544
Author: "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 ) );
}
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads