new[] causing segfault (never mind)

Recommended Posts

NEVER MIND! I found the problem! Here is the structure I'm using:
struct GBspSurfExtraFile			// version 1.02
{
float	BoundPos[3];			// boundary sphere position
float	chromeValue;
float	grazingValue;
COLORREF chromeColor;
DWORD	NumVI;					// # of vertex indices
DWORD	NumTris;				// # of triangles
WORD *VI;
WORD *Tris;
Then I'm declaring an array of these structures like this:
extrasurfinfo = new GBspSurfExtraFile[num];
Then I'm reading in values for the structure like this:
for (i = 0; i < num; i++)
{

if (extrasurfinfo[i].NumVI > 0)
{
extrasurfinfo[i].VI = new WORD[extrasurfinfo[i].NumVI];
}
else
extrasurfinfo[i].VI = NULL;

if (extrasurfinfo[i].NumTris > 0)
{
extrasurfinfo[i].Tris = new WORD[extrasurfinfo[i].NumTris];
}
else
extrasurfinfo[i].Tris = NULL;
}
read2bytes() and read4bytes() are just wrappers for fread(). The problem is that after a few iterations of this loop I get a segfault at that first new[]. I'm newing a resonable amount (usually 4 or 8 words or something like that) so it's not like I'm requesting 10 GB of data. When I run this through Valgrind it actually says the impossible happened. Anyone have any idea what's going on? The code looks perfectly fine to me. The problem was that I didn't new[] the second array as NumTris * 3, only NumTris.

Share on other sites
Have you stepped through it in the debugger to ensure that you're actually requesting the amount you think you're requesting?

Create an account

Register a new account

• Forum Statistics

• Total Topics
628283
• Total Posts
2981823

• 10
• 10
• 11
• 17
• 15