Sandundhammika's Blog











{March 28, 2010}   Hard to find Bugs hidden by C preprocessor

Last night I get hard time with my finding a bug.
that bug is hided by the C preprocessor.I just only use a simple macro shown
bellow.

#define drawOneLine(x1,y1,x2,y2) \
	glBegin(GL_LINES); \
	glVertex2d((x1),(y1)); \
	glVertex2d((x2),(y2)); \
	glEnd();

and this is the program that I tries to correct.

#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>

#define drawOneLine(x1,y1,x2,y2) \
	glBegin(GL_LINES); \
	glVertex2d((x1),(y1)); \
	glVertex2d((x2),(y2)); \
	glEnd();
	
void init()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glShadeModel(GL_FLAT);
}

void display()
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,1.0,1.0);
	glEnable(GL_LINE_STIPPLE);
	
	glLineStipple(1,0x0101); /* dotted */
	drawOneLine(50.0 ,125.0,150.0,125.0);
	glLineStipple(1,0x00FF);
	drawOneLine(150.0,125.0,250.0,125.0);
	glLineStipple(1,0x1C47);
	drawOneLine(250.0,125.0,350.0,125.0);
	

	/* in 2d row , 3 wide lines , each with different stipple */
	glLineWidth(5.0);
	glLineStipple(1,0x0101);
	drawOneLine(50.0,100.0,150.0,100.0);
	glLineStipple(1,0x00FF);
	drawOneLine(150.0,100.0,250.0,100.0);
	glLineStipple(1,0x1C47);
	drawOneLine(250.0,100.0,350.0,100.0);
	glLineWidth(1.0);
	
	/* in third row , 6 lines with dash/dot/dash stipple */
	/* as part of a singl connected line strip */
	glLineStipple(1.0,0x1C47);
	glBegin(GL_LINE_STRIP);
		for ( i=0;i < 7;i++)
			glVertex2f(50.0+((GLfloat) i * 50.0),75.0);
			
	glEnd();
	
	i=0;
	
	/* in  4th row , 6 independent lines with same stipple */
	for (i=0;i<6;i++)
		drawOneLine(50.0+((GLfloat)i*50.0),50.0,50.0 +((GLfloat)(i+1)*50.0),50.0);
	
	/* in 5th row, one line with dash/dot/dash stipple */
	/* and a stipple repeat factor of 5 */
	glLineStipple(5,0x1C47);
	drawOneLine(50.0,25.0,350.0,25.0);
	glDisable(GL_LINE_STIPPLE);
	glFlush();

}


void reshape(int w,int h)
{
	glViewport(0,0,(GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, (GLdouble) w,0.0, (GLdouble)h,-1.0,1.0);
}


int main(int argc,char**argv)
{
	
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(400,150);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}

The bug is marked in red.That line should be aligned with the rest of 5 lines too.
So everything in the program looks fine,and according to that imaginary something like this
cannot be happen.So where is the wrong.Can you guess?

Actually to pinpoint the error you need to lookup the preprocessor output.
This is how you should get the preprocessor output.and open it by notepad to view it.

J:\EXERCISES\opengl\glut\ch07>gcc -E ch07.c > preprocessed.cpp
ch07.c:90:2: warning: no newline at end of file

J:\EXERCISES\opengl\glut\ch07>notepad preprocessed.cpp

The -E does simply said stop in the preprocessor stage.and this will output the output to the
standard output, using ‘>’ operator I just simply redirect that output to a file called preprocessed.cpp.

So let’s go and see what is the wrong at preprocessed.cpp file.Preprocessed.cpp file is around 2000
line of code.Because every opengl helder file context is there.Anyway I will show where the error
is located.


 for (i=0;i<6;i++)
  glBegin(0x0001); glVertex2d((50.0+((GLfloat)i*50.0)),(50.0)); glVertex2d((50.0 +((GLfloat)(i+1)*50.0)),(50.0)); glEnd();;

So asking for you, do you get the error?still not get it? Oky then let me correct it.


 for (i=0;i<6;i++){
  glBegin(0x0001); glVertex2d((50.0+((GLfloat)i*50.0)),(50.0)); glVertex2d((50.0 +((GLfloat)(i+1)*50.0)),(50.0)); glEnd();;
}

So the curly braces should be applied , otherwise the glBegin(0x0001) only be nested
inside the for loop. so the source code file should be corrected as bellow.

	/* in  4th row , 6 independent lines with same stipple */
	for (i=0;i<6;i++){
		drawOneLine(50.0+((GLfloat)i*50.0),50.0,50.0 +((GLfloat)(i+1)*50.0),50.0);
	}

And finally it runs well.

So one coding advice , be careful with the preprocessor when you do code with C/C++

–happy coding–

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

et cetera
%d bloggers like this: