Skip to main content
GameDev.net gamedev.net
🔒 Locked

Declare array - Segmentation Fault

Started by Klaser Nov 23, 2004 at 4:50 PM 9 replies 1k views
Original Post
Klaser
Klaser
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
nmi
nmi
post your code
Ravyne
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.
throw table_exception("(? ???)? ? ???");
Klaser
Klaser
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 :)
chollida1
chollida1
Well if all your doing is declaring it then that might be the problem. You also need to define it.

Cheers
Chris
CheersChris
Klaser
Klaser
Hi chollida1!

Even though i dont use it? If I in my headerfile write "int test[500];" or "list 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
chollida1
chollida1
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
iMalc
iMalc
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
Jingo
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.
Klaser
Klaser
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
Ravyne
Ravyne
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.
throw table_exception("(? ???)? ? ???");

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.