www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/25/16:46:36

Newsgroups: comp.os.msdos.djgpp
From: Elliott Oti <e DOT oti AT stud DOT warande DOT ruu DOT nl>
Subject: Re: I'm going slightly mad!!
Sender: usenet AT fys DOT ruu DOT nl (News system Tijgertje)
Message-ID: <34F50789.3AB6@stud.warande.ruu.nl>
Date: Thu, 26 Feb 1998 06:11:21 GMT
References: <Pine DOT OSF DOT 3 DOT 91 DOT 980224095752 DOT 136A-100000 AT leofric>
Mime-Version: 1.0
Organization: Bipolar Widgets International
Lines: 116
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

My guess would be you're writing to invalid pointers somewhere.

I have made some dumb mistakes in my time, and I hope you will forgive
the liberty of
sharing one with you. Who knows, it may help. I call it:

 "Reading integer offsets from files and forgetting that arrays in C
begin from 0."

and it goes like this:

I read an array of N points from a 3d object file.
Then I read a triangle in. It is composed of points 15, 16, and 17 of my
array.
At least, that's what the file says. So I say: 
   triangle T = { Array[15], Array[16], Array[17] }

The problem is, the 15'th point of my array is Array[14], the 16th point
is Array[15],
and so on. ( The first point is Array[0], of course ).

So if you read N points from a file, and a triangle happens to have the
Nth point as
a vertex, you assign it Array[N]. Problem is, Array stops at Array[N-1].
(There comes your GPF, under certain circumstances.)

Now accessing Array[N] does not always cause a crash. Under Win 95, or
with malloc'ed memory, it will often happen that your C program can
access Array[N] without crashing
(I suppose because malloc allocates in block increments). But the number
contained in it
will be random trash, of course, most of the time. When the triangle
with the bogus vertex is to be rendered, in goes a random bit pattern
into your number-cruncher,
and out comes a FPE.

The chances are of course, that your problem is an entirely different
bug. I must say 
this particular bug wasted me an entire afternoon and I discovered it
only after 
totally rewriting the offending function. But I hope it spurs you into
examining your
code in another light. There's a subtle bug lurking somewhere in it.

Cheers,
-- 
------------ Elliott Oti ---------------
   ------------- http://www.fys.ruu.nl/~oti  ---------


Andy Maddison wrote:

> 
> I've been using DJGPP for a while now, just basic C++ 3d graphics
> programming, and everything has been going quite well.  But now I've run
> into some strange problems:
> 
> When I execute my current piece of work I sometimes get a General
> Protection Fault or Floating Point Error, but not everytime!
> So why does an unchanged program crash periodically?
> 
> Stranger still:  I've had the above errors occur when I've simply invoked
> gcc to compile my program (for the first time), i.e. gcc crashed without
> compiling, but on subsequent attempts all went well with compilation and
> linking (gxx).  But then my program crashed again.
> 
> I've used gdb which imforms me where the error occurs but I've checked
> the code and there shouldn't be a problem, I've called the same function
> many times before with the same argument types and all went as expected.
> 
> I've also had an older program crash (with similar errors) when I
> executed it during the same session, but it has always worked with no
> problems.
> 
> The current program reads some tri-mesh data from a text file, i.e.
> vertex and face information, and creates new instances of vertices (200+)
> and polygons (400+).  The number of vertices and faces is read at the
> beginning of the file.  So the code looks something like this:
> 
> read number of vertices and faces from file
> 
> Vertex** v = new Vertex*[vertices];
> Polygon** p = new Polygon*[faces];
> 
> for(int i=0;i<vertices;i++)
> {
>         read vertex coordinates x,y,z from file
>         v[i] = new Vertex(x,y,z);
> }
> 
> for(int i=0;i<faces;i++)
> {
>         read polygon vertices v1,v2,v3 from file
>         p[i] = new Polygon(v[v1],v[v2],v[v3]);
> }
> 
> I've inserted statements within the loop to print out the data as it is
> read just to double check that it is being read correctly - and it is!
> 
> Could the problem be memory related?
> 
> I'm running Windows95 and I've got 32Mb in my machine, is this automatically
> used by my program?
> 
> Due to the use of edge lists for storing the polygon's data, each polygon
> takes up in excess of 3k.  But I'm freeing this memory when each
> polygon is destroyed.
> 
> The funny thing is that I've tried commenting out various different parts of
> the code and recompiling, but I can't seem to narrow the error down to a
> specific line of code.  I've also tried using hard coded values for the
> vertices and polygons, i.e. not reading from the file at all.
> 
> If you can throw some light on this situation, then please do.
> 
> Thanks.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019