LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-02-2006, 09:04 AM   #1
Ed_999
LQ Newbie
 
Registered: Sep 2004
Distribution: Slackware RedHat Fedora
Posts: 27

Rep: Reputation: 15
Linux C Segmentation Error, dah?


Linux C question using the gcc compiler.
gcc -Wall -W my_c_code.c -o ouput_program
How do you deal with a segmentation error?
I get this segmentation error at compile time
so is it a syntax error of some kind?
There is no clue as to what line of code is
causing it, so the entire program is suspect.
It drives me nuts! How do you get rid of a
segmentation error?
 
Old 05-02-2006, 09:28 AM   #2
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
if

gcc -Wall -W my_c_code.c -o ouput_program

is producing a segmentation fault, then it the problem is not in your program. Since its not being executed yet and segmentation faults occur at runtime.

This means that your gcc is getting a segmentation fault. This could possibly be because of corrupted installation of gcc or libraries.
 
Old 05-02-2006, 09:32 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I would not like to comment until I could see the code. As its only one source file could you please post it, or dump it at a location in my signature.
thanks
 
Old 05-02-2006, 09:36 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Like sibtay said: you've got a corrupted and/or incompatible version of gcc:

1. Find a version of gcc that matches your platform, OS and OS version
(for example, go to www.rpmfind.org)

2. De-install your current version. Reboot your PC.

3. Install the new, "known good" version of gcc.
 
Old 05-02-2006, 09:38 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Hmmm interesting that two people think it's gcc. As i always use an ide and never the command line, could please explain why it's gcc and not the code?

ty
 
Old 05-02-2006, 09:50 AM   #6
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
What exactly is getting the SIGSEGV? Is it the compiler, during compilation, which may leave a large "gcc.core" file behind? Or is it your program, during execution, which probably leaves an "output_program.core" file? If it is gcc, which seems very very unlikely to me (unless you did some bleeding edge CVS installation), you may need to file a bug report. Otherwise try testing your output_program with a debugger: `gdb ./output_program` and type "run".

To dmail: a compiler / interpreter should never crash during compilation / execution. It'd be like your IDE crashing in the middle of a session. It shouldn't happen no matter how bad the code you pass into it is. The compiler exists to catch those sort of errors! And if it just dies then it's not doing its job.
 
Old 05-02-2006, 10:06 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by dmail
Hmmm interesting that two people think it's gcc. As i always use an ide and never the command line, could please explain why it's gcc and not the code?
Because TS indicates that it's at compile time and not at runtime He/She might have informed us incorrectly, but till that time I agree that it's not the compiled program as it's not running and so it must be the compiler.
 
Old 05-02-2006, 10:10 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Thanks I see it now.
I'm sure when I use an ide i can get seg faults, but this maybe due to the build in debugger.

I just ran the following from the command line and got no warnings so I will stay quite on this matter lol.

Code:
int main(int argc, char *argv[])
{
	int* a;
	a++;
	return 0;
}
Quote:
gcc -Wall test.c -o test
 
Old 05-04-2006, 11:42 AM   #9
Ed_999
LQ Newbie
 
Registered: Sep 2004
Distribution: Slackware RedHat Fedora
Posts: 27

Original Poster
Rep: Reputation: 15
Linux C Segmentation Error, dah?

I got it fixed. There is nothing wrong with
gcc or even my ancient computer. It was the code.
I can't remember exactly what I did since I have
a habbit of panicing and trying everything at
once, but I think the problem was this:
I won't write all the code, only the stuff that
matters:
Defined a structure globally
struct my_structure {
float * big_array;
other stuff...
}
typedef struct my_structure STX;

void initialize_structure( STX * stxp )
{
float * temp_array;
temp_array = (float *)malloc( sizeof(float) * 200 );

This next line caused the segmentation error
stxp->big_array = temp_array;

I fixed this in main()
int main(void)
{
STX * my_structure_ptr;
The next two lines fixed the problem
STX the_structure;
my_structure_ptr = &the_structure;

Real dumb mistake! I should have caught it
right alway, but I suspected the everything else,
including the computer hardware. It's kind of
hard to admit to a mistake like this. It's so
stupid!
While changing code all over the program and
recompiling, I got all kinds of wierd errors.
One was even from a library I never heard of
before. ( Id.so or something like that )
I should have written it down.
I use the command line and the nano editor.
For debugging I use printf and /* */ all over.
Sometimes gdb or DDD. The IDEs I've tried
all seem to take a year to "configure" and
my little programs aren't worth the trouble.
Thanks to you all for your help.
 
  


Reply

Tags
linux, programming, segmentation fault


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation error harbir Programming 5 07-26-2004 03:10 PM
Segmentation error - WHAT??????????? the_rydster Linux - Laptop and Netbook 1 11-16-2003 05:40 PM
"Segmentation Fault" error message when trying to run Yahoo Messenger on Linux 8.0 niranjanpage Linux - Software 6 10-27-2003 01:21 AM
Segmentation Error WNxSquee Linux - Software 1 08-15-2003 12:48 PM
Segmentation Error Roy-Svork Linux - Software 1 01-08-2003 12:21 AM


All times are GMT -5. The time now is 07:02 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration