Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Getting a "Segmentation Fault" when running on Linux

Reply
Thread Tools

Getting a "Segmentation Fault" when running on Linux

 
 
James Kuyper
Guest
Posts: n/a
 
      01-02-2012
On 01/01/2012 03:57 PM, Maxx wrote:
> On Dec 29 2011, 9:49�am, Barry Schwarz <(E-Mail Removed)> wrote:
>> On Thu, 29 Dec 2011 07:28:16 -0800 (PST), Maxx

....
>>> � � � �errExit("opening file %s", argv[1]);

>>
>> Where is this function declared?
>>
>>

> This custom error functions are all declared in "tlpi_hdr.h". and they
> work fine i've tested them in other programs


Does removing the custom error function calls remove the segmentation fault?

If so, then the problem involves those function calls, even if the
problem isn't actually in those functions, and we therefore need to know
how they work before we can help you.

If not, they are a complication that can be removed without removing the
ability to demonstrate the problem. As a general rule, you should make
the program which demonstrates a problem as small as possible. This
generally makes it easier to determine what the problem actually is. In
fact, I've frequently solved a problem as a side effect of simplifying
the code demonstrating the problem.
--
James Kuyper
 
Reply With Quote
 
 
 
 
Geoff
Guest
Posts: n/a
 
      01-02-2012
On Sun, 1 Jan 2012 12:57:14 -0800 (PST), Maxx
<(E-Mail Removed)> wrote:

>> > * *if (outputFd = -1)


Should this not be: if (outputFd == -1)?
 
Reply With Quote
 
 
 
 
Maxx
Guest
Posts: n/a
 
      01-02-2012


On Thursday, December 29, 2011 8:18:48 PM UTC+3, Philip Lantz wrote:
> Maxx wrote:
>
> > ... when i attempt to run
> > it i get a segmentation fault.
> >
> > ./exrcs401<ename.c.inc tfile
> > Segmentation fault.
> >
> > exrcs401 is the name of the program
> > ename.c.inc is the input file
> > tfile is the output file.
> >
> >
> > if (argc < 2 || strcmp(argv[1], "--help") == 0)
> > usageErr("%s input file or {-a:append}...\n",
> > argv[0]);
> >
> > if (strcmp(argv[2], "-a") == 0)

>
> argv[2] is null.


Yeah i fixed this error in the new one that ive written..
 
Reply With Quote
 
Philip Lantz
Guest
Posts: n/a
 
      01-02-2012
Gordon Burditt wrote:
>
> > Thats the thing i tried using printf() almost everywhere in the
> > program even right in the part after all the declarations are but the
> > program still gives Segmentation fault without printing any other
> > message whatsoever..

>
> Either use fprintf(stderr, .... ) or litter your program with fflush(stdout)
> calls. A segmentation fault is not guaranteed to fflush(stdout) for
> you, and it usually doesn't.


Another possible reason that you don't see any output before the
segmentation fault is that when you add a printf, the printf itself is
causing a fault, by trying to print a string where the pointer argument
to printf is null or uninitialized. (Many printf implementations will
detect a null pointer and print "(null)", but it's not required, since
the behavior is undefined.)
 
Reply With Quote
 
Maxx
Guest
Posts: n/a
 
      01-02-2012
On Jan 1, 6:40*pm, James Kuyper <(E-Mail Removed)> wrote:
> On 01/01/2012 03:57 PM, Maxx wrote:
>
> > On Dec 29 2011, 9:49 am, Barry Schwarz <(E-Mail Removed)> wrote:
> >> On Thu, 29 Dec 2011 07:28:16 -0800 (PST), Maxx

> ...
> >>> errExit("opening file %s", argv[1]);

>
> >> Where is this function declared?

>
> > This custom error functions are all declared in "tlpi_hdr.h". and they
> > work fine i've tested them in other programs

>
> Does removing the custom error function calls remove the segmentation fault?
>
> If so, then the problem involves those function calls, even if the
> problem isn't actually in those functions, and we therefore need to know
> how they work before we can help you.
>
> If not, they are a complication that can be removed without removing the
> ability to demonstrate the problem. As a general rule, you should make
> the program which demonstrates a problem as small as possible. This
> generally makes it easier to determine what the problem actually is. In
> fact, I've frequently solved a problem as a side effect of simplifying
> the code demonstrating the problem.
> --
> James Kuyper


Those custom error function actually works fine, i've used them in
numerous other programs. Yeah i know this all made this program
complicated but i wasn't sure which part to ommit to make this more
simplified.
 
Reply With Quote
 
Maxx
Guest
Posts: n/a
 
      01-02-2012
On Jan 1, 10:00*pm, Geoff <(E-Mail Removed)> wrote:
> On Sun, 1 Jan 2012 12:57:14 -0800 (PST), Maxx
>
> <(E-Mail Removed)> wrote:
> >> > * *if (outputFd = -1)

>
> Should this not be: if (outputFd == -1)?


Oh another mistake, will fix it.
Thanks
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-02-2012
Maxx <(E-Mail Removed)> writes:

> On Jan 1, 10:00*pm, Geoff <(E-Mail Removed)> wrote:
>> On Sun, 1 Jan 2012 12:57:14 -0800 (PST), Maxx
>>
>> <(E-Mail Removed)> wrote:
>> >> > * *if (outputFd = -1)

>>
>> Should this not be: if (outputFd == -1)?

>
> Oh another mistake, will fix it.


Not anew one, though. It was pointed out about four days ago.

--
Ben.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      01-02-2012
On 01/02/2012 04:56 AM, Maxx wrote:
> On Jan 1, 6:40�pm, James Kuyper <(E-Mail Removed)> wrote:
>> On 01/01/2012 03:57 PM, Maxx wrote:
>>
>>> On Dec 29 2011, 9:49 am, Barry Schwarz <(E-Mail Removed)> wrote:
>>>> On Thu, 29 Dec 2011 07:28:16 -0800 (PST), Maxx

>> ...
>>>>> errExit("opening file %s", argv[1]);

>>
>>>> Where is this function declared?

>>
>>> This custom error functions are all declared in "tlpi_hdr.h". and they
>>> work fine i've tested them in other programs

>>
>> Does removing the custom error function calls remove the segmentation fault?
>>
>> If so, then the problem involves those function calls, even if the
>> problem isn't actually in those functions, and we therefore need to know
>> how they work before we can help you.
>>
>> If not, they are a complication that can be removed without removing the
>> ability to demonstrate the problem. As a general rule, you should make
>> the program which demonstrates a problem as small as possible. This
>> generally makes it easier to determine what the problem actually is. In
>> fact, I've frequently solved a problem as a side effect of simplifying
>> the code demonstrating the problem.
>> --
>> James Kuyper

>
> Those custom error function actually works fine, i've used them in
> numerous other programs. ...


You're missing the point: even if those functions are perfectly written,
removing them could cause the symptoms of your actual problem to change
or even disappear. If so (which, so far, you've neither confirmed nor
denied), that would mean that the real problem involves an interaction
between those functions and the rest of the program. For instance,
removing them might change the relative location in memory of two other
objects, and the symptoms you're seeing might occur only when they are
exactly 13533 bytes apart.

> ... Yeah i know this all made this program
> complicated but i wasn't sure which part to ommit to make this more
> simplified.


The answer to that is simple. Before taking your program to someone else
for help, remove everything that doesn't make the symptoms of your
problem disappear - but make sure, after removing each item, that the
symptoms still occur. NOTE: do the removals on a copy of your program,
not on the original. The simpler the program is, the easier it will be
for the person who's helping you to identify what the real problem is.
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting GUI for ruby for Linux running (QT or wxWidget)? Markus Fischer Ruby 22 09-25-2010 01:27 PM
Re: Is Netscape Leaving Firefox Behind on GNU/Linux? THE LINUX PROPAGANDA MACHINE CONTINUES. FIREFOX IGNORING LINUX............. traci.manicotti@gmail.com Computer Support 2 10-20-2007 02:12 PM
Linux... yeah linux.. Linux Have a nice cup of pee NZ Computing 19 04-17-2006 10:16 AM
Want to dual boot XP & Linux - Getting new PC - Will it work with Linux? Alan NZ Computing 55 09-11-2005 12:50 AM
Getting the KVM running on ARM Linux on an ARM processor based device Steve Jasper Java 0 11-20-2003 06:55 PM



Advertisments