PDA

View Full Version : Error in http://www.opengl.org/.../redbook/tess.c



MUFTI
03-19-2010, 11:54 AM
It looks like, there is a memory error in the tess.c
redbook example on the www.opengl.org:
http://www.opengl.org/resources/code/samples/redbook/tess.c

vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));

vertex[0] = coords[0];
vertex[1] = coords[1];
vertex[2] = coords[2];

vertex[i] = weight[0] * vertex_data[0][i]
+ weight[1] * vertex_data[1][i]
+ weight[2] * vertex_data[2][i]
+ weight[3] * vertex_data[3][i];

As you can see, this code fragment accesses

1. byte: vertex[0]
2. byte: vertex[1]
3. byte: vertex[2]

and

4. byte: vertex[3]
5. byte: vertex[4]
6. byte: vertex[5]
7. byte: vertex[6]

So 7 bytes are accessed, but only 6 bytes are allocated.

If you would change

vertex = (GLdouble *) malloc(6 * sizeof(GLdouble));

to

vertex = (GLdouble *) malloc(7 * sizeof(GLdouble));

a other problem would occure:

The routine would try to read 4 bytes after each vertex
of the "star" arrays, but only 3 bytes are available:

GLdouble star[5][6] = {250.0, 50.0, 0.0, 1.0, 0.0, 1.0,
325.0, 200.0, 0.0, 1.0, 1.0, 0.0,
400.0, 50.0, 0.0, 0.0, 1.0, 1.0,
250.0, 150.0, 0.0, 1.0, 0.0, 0.0,
400.0, 150.0, 0.0, 0.0, 1.0, 0.0};

So the routine in general would try to read one byte more
then the length of the "star" arrays.

So i think, the proper solution would be

for (i = 3; i < 6; i++)

instead of

for (i = 3; i < 7; i++)

so long
MUFTI