[Beginner]Seg Fault

This is a discussion on [Beginner]Seg Fault within the C++ Programming forums, part of the General Programming Boards category; Hello, My code pasted below is receiving random segmentation faults. I am not sure I took the right approach for ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    [Beginner]Seg Fault

    Hello,

    My code pasted below is receiving random segmentation faults. I am not sure I took the right approach for a dynamic sized buffer of Triangle objects(3 Vector3f(3 floats)). Any idea's would be greatly appreciated.

    The fault happens at the first write after entering the loop but only happens in glx_base code off Nehe's OGL site. This class works flawless all day long in the console.

    Code:
    int cMesh::BuildMesh(cMCNK *pMCNK)
    {
    	sTriangle *thisTri = NULL;
    	sObject *thisObj = NULL;
    	sMCNK_Block MCNK = pMCNK->GetMCNK_Block();
    	char *buffer = NULL;
    	buffer = new char[sizeof(sTriangle) * 256];
    	memset(buffer, '\0', sizeof(sTriangle) * 256);
    	thisTri = (sTriangle*)buffer[0];
    	thisObj = new sObject;a
    	memset(thisObj, '\0', sizeof(sObject));
    	thisObj->Triangle = thisTri;
    	thisObj->numTriangles = 256;
    	thisObj->pNext = NULL;
    	mesh->Object = thisObj;
    	std::cout << mesh->Object->numTriangles;
    	int OffsetX = (MCNK.MCNK.position.X);
    	int OffsetY = (MCNK.MCNK.position.Z);
    	int OffsetZ = (MCNK.MCNK.position.Y);
    	
    	for(int y = 0; y < 8; y++)
    	{
    		for(int x = 0; x < 0; x++)
    		{
    			
    			//First Vertex: Pos 1
    			thisTri->Vertex[0].X = ((OffsetX)+((x * 3.703703704)));
    			thisTri->Vertex[0].Y = ((OffsetY)+pMCNK->GetNoLod(x,y));
    			thisTri->Vertex[0].Z = ((OffsetZ)+((y * 3.703703704)));	
    
    			//Second Vertex: Pos 2
    			thisTri->Vertex[1].X = ((OffsetX)+(((x + 1) * 3.703703704)));
    			thisTri->Vertex[1].Y = ((OffsetY)+pMCNK->GetNoLod((x + 1), y));
    			thisTri->Vertex[1].Z = ((OffsetZ)+((y * 3.703703704)));
    
    			//Third Vertex: Pos 10
    			thisTri->Vertex[2].X = ((OffsetX)+(((x + 0.5) * 3.703703704)));
    			thisTri->Vertex[2].Y = ((OffsetY)+pMCNK->GetLod(x, y));
    			thisTri->Vertex[2].Z = ((OffsetZ)+(((y + 0.5) * 3.703703704)));	
    			thisTri = thisTri + sizeof(thisTri);
    
    			//First Vertex: Pos 2
    			thisTri->Vertex[0].X = ((OffsetX)+(((x + 1) * 3.703703704)));
    			thisTri->Vertex[0].Y = ((OffsetY)+pMCNK->GetNoLod((x + 1), y));
    			thisTri->Vertex[0].Z = ((OffsetZ)+((y * 3.703703704)));	
    
    			//Second Vertex: Pos 19
    			thisTri->Vertex[1].X = ((OffsetX)+(((x + 1) * 3.703703704)));
    			thisTri->Vertex[1].Y = ((OffsetY)+pMCNK->GetNoLod((x+1), (y+1)));
    			thisTri->Vertex[1].Z = ((OffsetZ)+(((y + 1) * 3.703703704)));
    
    			//Third Vertex: Pos 10
    			thisTri->Vertex[2].X = ((OffsetX)+(((x + 0.5) * 3.703703704)));
    			thisTri->Vertex[2].Y = ((OffsetY)+pMCNK->GetLod(x, y));
    			thisTri->Vertex[2].Z = ((OffsetZ)+(((y + 0.5) * 3.703703704)));	
    			thisTri = thisTri + sizeof(thisTri);
    
    			//First Vertex: Pos 19
    			thisTri->Vertex[0].X = ((OffsetX)+(((x+1) * 3.703703704)));
    			thisTri->Vertex[0].Y = ((OffsetY)+pMCNK->GetNoLod((x+1), (y+1)));
    			thisTri->Vertex[0].Z = ((OffsetZ)+(((y+1) * 3.703703704)));	
    
    			//Second Vertex: Pos 18
    			thisTri->Vertex[1].X = ((OffsetX)+((x * 3.703703704)));
    			thisTri->Vertex[1].Y = ((OffsetY)+pMCNK->GetNoLod(x, (y+1)));
    			thisTri->Vertex[1].Z = ((OffsetZ)+(((y+1) * 3.703703704)));
    
    			//Third Vertex: Pos 10
    			thisTri->Vertex[2].X = ((OffsetX)+(((x + 0.5) * 3.703703704)));
    			thisTri->Vertex[2].Y = ((OffsetY)+pMCNK->GetLod(x, y));
    			thisTri->Vertex[2].Z = ((OffsetZ)+(((y + 0.5) * 3.703703704)));	
    			thisTri = thisTri + sizeof(thisTri);
    			
    			//First Vertex: Pos 1
    			thisTri->Vertex[0].X = ((OffsetX)+((x * 3.703703704)));
    			thisTri->Vertex[0].Y = ((OffsetY)+pMCNK->GetNoLod(x, y));
    			thisTri->Vertex[0].Z = ((OffsetZ)+((y * 3.703703704)));	
    
    			//Second Vertex: Pos 18
    			thisTri->Vertex[1].X = ((OffsetX)+((x * 3.703703704)));
    			thisTri->Vertex[1].Y = ((OffsetY)+pMCNK->GetNoLod(x, (y+1)));
    			thisTri->Vertex[1].Z = ((OffsetZ)+(((y+1) * 3.703703704)));
    
    			//Third Vertex: Pos 10
    			thisTri->Vertex[2].X = ((OffsetX)+(((x + 0.5) * 3.703703704)));
    			thisTri->Vertex[2].Y = ((OffsetY)+pMCNK->GetLod(x, y));
    			thisTri->Vertex[2].Z = ((OffsetZ)+(((y + 0.5) * 3.703703704)));	
    			thisTri = thisTri + sizeof(thisTri);
    			
    		}
    	}
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Code:
    	char *buffer = NULL;
    	buffer = new char[sizeof(sTriangle) * 256];
    	memset(buffer, '\0', sizeof(sTriangle) * 256);
    	thisTri = (sTriangle*)buffer[0];
    	thisObj = new sObject;a
    	memset(thisObj, '\0', sizeof(sObject));
    This kind of code is just plain ugly in a C++ program.

    What is buffer?
    If you wanted an array of triangles, why didn't you declare it with that type?
    Casting one pointer to another could be landing you with alignment exceptions.

    Further, I guess sObject is a class of some sort, in which case using memset to initialise it is a disaster. Use a constructor!




    > for(int x = 0; x < 0; x++)
    Does this loop even run at all?
    Assuming that this is a typo (and this isn't your real code at all), then what is to stop this loop exceeding 256 entries (the magic number in your code above).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    And be aware of the strict aliasing rule, though I believe in this specific case it's fine (doesn't mean it's a good idea).

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    All of the objects proceeded by s are structs and to the best of my knowledge the only way to null them is memset?

    Also the loop was 8 in the code, I missed that before posting here.

    I will change it back to a straight array which I was still having the same issue with.(256 is only a magic number until I get the code block working and implement a way for it to count prior to building the object)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    >>thisTri = (sTriangle*)buffer[0];
    Why are you doing this? If you need some triangles, make some! But don't create some characters and pretend they're a triangle!
    You're also setting yourself up for some hurt since you're using raw pointers. And what's wrong with std::vector?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Ok do I have a huge misunderstanding on the std::vector class is it slow to read from?

    I was under the impression in C# it worked similar to linked lists.

    I wrote a small project using vectors and it appears when I call reserve it stores stuff in sequential order in memory.

    The idea which I thought was the old way to do things in C when absolute speed is needed is to create a buffer of any kind in memory the size or larger then you needed and use pointers to read and write raw data. However these is very dangerous I understand.

    If I am reading thousands of triangles per second with a std::vector and the .at() method how is this going to hold up? I am testing it atm, but the application is only reading 256 triangles for simplicity.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,630
    1) Vector and linked lists are two different things. Vectors are dynamic arrays. Data is stored contiguous in memory.
    2) Vector is not slow to read from. It's just as fast as from a normal array. It cannot possibly be slower unless you are using the .at member function which performs a check to see if you access out-of-bounds.
    3) Don't use any type--use the type you need. C++ is a type-strong language. Messing with chars and raw buffers is dangerous and not recommended.
    4) Why don't you try with .at, with the index operator and w/o vector? Empirical results are the best, no?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,773
    Quote Originally Posted by the4bidn
    Ok do I have a huge misunderstanding on the std::vector class is it slow to read from?

    I was under the impression in C# it worked similar to linked lists.
    No, std::vector is a dynamic array container.

    Quote Originally Posted by the4bidn
    I wrote a small project using vectors and it appears when I call reserve it stores stuff in sequential order in memory.
    Yes, though with reserve the objects do not actually exist; it is only space that is reserved for them.

    Quote Originally Posted by the4bidn
    The idea which I thought was the old way to do things in C when absolute speed is needed is to create a buffer of any kind in memory the size or larger then you needed and use pointers to read and write raw data. However these is very dangerous I understand.
    It is not really dangerous if you do it right. Unfortunately, mistakes do happen. Anyway, with std::vector you can make the same kind of mistakes, except for the mistake of forgetting to free the memory.

    Quote Originally Posted by the4bidn
    If I am reading thousands of triangles per second with a std::vector and the .at() method how is this going to hold up?
    You have to test and compare with your timing requirements. Admittedly, with at() boundary checking is performed, so the danger you mentioned is reduced, but the speed would be affected as well, yet it may still be fast enough.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Alright, thanks for the advice. I am gonna try use std::vector.at() unless I notice a performance hit being to large. As of now at ~50,000 triangles it still seems to be doing alright.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21