Declare array - Segmentation Fault

Started by
9 comments, last by ravyne2001 19 years, 4 months ago
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
Advertisement
post your code
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.

throw table_exception("(? ???)? ? ???");

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 :)
Well if all your doing is declaring it then that might be the problem. You also need to define it.

Cheers
Chris
CheersChris
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
Where does it seg fault??

There has to be a line if its a run-time bug??

Showing your code may help:)

Cheers
Chris
CheersChris
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
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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.
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

This topic is closed to new replies.

Advertisement