Sign in to follow this  
Klaser

Declare array - Segmentation Fault

Recommended Posts

Klaser    122
Hi, I am making a software renderer for a school project. I have this really wierd/anoying problem: If I declare an array in one of my *.h files and compile I suddenly get a segmentation fault (using g++ in unix), even though I dont actually use the array anywhere in my code. If I then delete all the *.o files and recompile, everything works perfect again. If I compile one more time the segmentation fault returns...doh!? I´ve had sort of the same problem in another project of mine (in DirectX)where i declared a new matrix and then the head of my animated .x model would disappear...I never solved that either. I really hope somebody can help me out here. I am really curious to what can cause this...must be some memory allocation problem...any ideas? Best regards, Klaus

Share this post


Link to post
Share on other sites
Ravyne    14300
You shouldn't be declaring anything in a header file. Declare it in the corresponding C or CPP file and extern it in the header.

Share this post


Link to post
Share on other sites
Klaser    122
Ok thanks I´ll try that tomorrow, I just cant see how it can give a segmentation fault though. I would really like to understand what exactly happens. I have a earlyer version of the project and it works fine there...it doesnt matter which *.h file i declare it in either.

Thanks for your help :)

Share this post


Link to post
Share on other sites
Klaser    122
Hi chollida1!

Even though i dont use it? If I in my headerfile write "int test[500];" or "list<CVector*> vp_list;" i´ll get a segmentation fault at runtime eventhough i dont use it anywhere else in my code. I dont understand why it will mess with the rest of the program.

Best regards, Klaus

Share this post


Link to post
Share on other sites
iMalc    2466
First of all, put asserts in anywhere that is suitable such as checking the bounds for every array you access.

If that doesn't find your bug then step through your code with a debugger to see where it fails.

If you still haven't got it, make a copy of your project and take out as much as you can without making the problem go away.

If still no luck then post this stripped down version of your code that still reproduces the fault, making sure you make use the [source] [/source] tags

Share this post


Link to post
Share on other sites
Jingo    582
Quote:
Original post by Ravyne
You shouldn't be declaring anything in a header file. Declare it in the corresponding C or CPP file and extern it in the header.


You have your terms mixed up there. You shouln't DEFINE the object in the header file, adding extern often turns it into a declaration.

Share this post


Link to post
Share on other sites
Klaser    122
Thanks for your replyes!

I tried declaring the array using extern in the headerfile and then defining it in the cpp file and it works - no more segmentation fault! BUT I cant use extern in class scope, so how do I declare it as member variable either private or public without using extern. As soon as i remove the extern identifyer the segmentation fault returns. Should it give this error when I do like that or could there be something wrong with my code...if so I have to start debugging like some of you suggest.

Best regards, Klaus

Share this post


Link to post
Share on other sites
Ravyne    14300
Quote:
Original post by Jingo
Quote:
Original post by Ravyne
You shouldn't be declaring anything in a header file. Declare it in the corresponding C or CPP file and extern it in the header.


You have your terms mixed up there. You shouln't DEFINE the object in the header file, adding extern often turns it into a declaration.


Right. My bad.

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