Sign in to follow this  
BradDaBug

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	BoundRadius;			// boundary sphere radius
	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++)
	{
		read4bytes(&extrasurfinfo[i], 9, fp);
		
		if (extrasurfinfo[i].NumVI > 0)
		{
			extrasurfinfo[i].VI = new WORD[extrasurfinfo[i].NumVI];	
			read2bytes(extrasurfinfo[i].VI, extrasurfinfo[i].NumVI, fp);
		}
		else
			extrasurfinfo[i].VI = NULL;
		
		if (extrasurfinfo[i].NumTris > 0)
		{
			extrasurfinfo[i].Tris = new WORD[extrasurfinfo[i].NumTris];
			read2bytes(extrasurfinfo[i].Tris, extrasurfinfo[i].NumTris * 3, fp);
		}
		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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this