Sign in to follow this  
steenreem

SIGSEGV error

Recommended Posts

Hi thanks for reading. When I run my program it crashes because of a null pointer being dereferenced. I know where in the code this happens, but I don't know why it happens. It happens in the second line of the functions drawAll, show here below: void drawAll(Shape s, Cor c){ List<Location>* templist = ShapeList(s,c); while((*templist).empty == false){ switch(s){ case Beam: drawBeam((*templist).value,c); break; case Panel: drawPanel((*templist).value,c); break; } templist = (*templist).next; if (templist==0){ printf("crap"); abort; } }; } The function is run multiple times and works about 10 times before it crashes. I don't understand why templist could be a null pointer. It is first set by Shapelist which never returns a null pointer, and it is updated within the while loop but this is not when it crashes. This is Shapelist: List<Location> XBeamList; List<Location> YBeamList; List<Location> ZBeamList; List<Location> XPanelList; List<Location> YPanelList; List<Location> ZPanelList; List<Location>* ShapeList(Shape s, Cor c){ List<Location>* ret; switch (c){ case X: switch (s){ case Beam: ret = &XBeamList; break; case Panel: ret = &XPanelList; break; } case Y: switch (s){ case Beam: ret = &YBeamList; break; case Panel: ret = &YPanelList; break; } case Z: switch (s){ case Beam: ret = &ZBeamList; break; case Panel: ret = &ZPanelList; break; } break; } return ret; } Any ideas or extra info you need? Thanks =)

Share this post


Link to post
Share on other sites
First of all, it sounds like a great time to get familiarized with your debugger, as these sorts of things are exactly what it is meant to find. Also, a SIGSEGV can be triggered by accessing memory that does not belong to you, not just accessing NULL, which means that an unassigned value would also result in such an error. The assumption is then that you are submitting some value for 'c' or some value for 's' that is not in the set {X,Y,Z} for 'c' or {Plane,Beam} for 's', and are thus falling through the switch statement and returning an unassigned value.

If you happen to not have a debugger handy, [Get one... now. In the mean time though...] initialize 'ret' to NULL, and check for null before returning. Throw an exception in the event of 'ret == NULL', and make sure that this exception has the working values of 'c' and 's'.

If you're interested in receiving more assistance, post what calls "drawAll()".

A useless bit of trivial: There are some systems that do not respond with a segmentation fault [SIGSEG] on accessing NULL, but only on accessing unassigned memory [with a dereference of NULL actually returning the VALUE zero]. These systems are weird though.... so don't worry about it too much.

Share this post


Link to post
Share on other sites
Based on what you've said, arguably Shapelist does return an invalid pointer (IIRC, a SIGSEGV is not necessarily a null pointer violation). Note that it will do this if s is not in {Beam, Panel} or c is not in {X, Y, Z}.

I would recommend adding some assertions to check your assumptions, and I would recommend learning to use your debugger.

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