Jump to content
  • Advertisement
Sign in to follow this  
BradDaBug

new[] causing segfault (never mind)

This topic is 5025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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, 9, fp);
		
		if (extrasurfinfo.NumVI > 0)
		{
			extrasurfinfo.VI = new WORD[extrasurfinfo.NumVI];	
			read2bytes(extrasurfinfo.VI, extrasurfinfo.NumVI, fp);
		}
		else
			extrasurfinfo.VI = NULL;
		
		if (extrasurfinfo.NumTris > 0)
		{
			extrasurfinfo.Tris = new WORD[extrasurfinfo.NumTris];
			read2bytes(extrasurfinfo.Tris, extrasurfinfo.NumTris * 3, fp);
		}
		else
			extrasurfinfo.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
Advertisement
Have you stepped through it in the debugger to ensure that you're actually requesting the amount you think you're requesting?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!