public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Segmentation fault Causes
@ 2003-02-24  5:39 Ajay Bansal
  2003-02-24  8:00 ` Sergei
  0 siblings, 1 reply; 7+ messages in thread
From: Ajay Bansal @ 2003-02-24  5:39 UTC (permalink / raw)
  To: gcc-help

John

Can you please tell me how to use this fprintf(stderr... . ) function?

Do I need to give it at many different places in the code??? If that is
the case, what's is the difference between putting many different
"cerr<<................"???

-Ajay


-----Original Message-----
From: J.C.Wren [mailto:jcwren@jcwren.com] 
Sent: Monday, February 24, 2003 10:52 AM
To: 'Daniel Bolgheroni'; gcc-help@gcc.gnu.org
Subject: RE: Segmentation fault Causes


	Most commonly it's caused by trying to write to a pointer that
either is uninitialized, or NULL.

int main (int argc, char **argv)
{
   char *p = NULL;
   *p = 'A';
}

will cause a segfault.  Also, if 'p' isn't assigned an explicit value,
it will have whatevers on the stack at that point.  Another thing that
will cause a segfault is writing passed the end of an array:

int main (int argc, char **argv)
{
   char buffer [16];
   memset (buffer, 0, 64);
}

will segfault.

	I would suggest compiling your program with the -W and -Wall
switches ('gcc -W -Wall myprog.c -o myprog').  Fix any and all warnings.
GCC is very good about warning about usage of uninitialized variables.

	If you want to see how far you're getting in your program use
'fprintf (stderr, "Got to line %d\n", __LINE__);'.  This will show how
far you've got before the segfault occurs.  You must write to stderr and
not stdout because of buffering.  When the program segfaults, unbuffered
I/O will NOT be written to the console.  This is why people often make
the mistake of saying 'printf ("Got to line %d\n", __LINE__);' and
thinking they really didn't get that far.  In reality, it did get that
far, but the I/O was lost when the buffer remained unflushed after the
segfault.  There are some exceptions to this rule, but it's not worth
pursuing at this point (just so someone does say "but if you set
unbuffered I/O mode, that doesn't apply!").

	--John


> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
> Behalf Of Daniel Bolgheroni
> Sent: Sunday, February 23, 2003 23:55
> To: gcc-help@gcc.gnu.org
> Subject: Segmentation fault Causes
>
>
> What are the causes that a program can give an error "Segmentation 
> fault (core dumped)"?
>
> I'm compiling a program I'm writing but I getting some of this error 
> messages executing it. I looked into gcc FAQ and gcc 2.95 manual, but 
> I found no references to this problem.
>
> I'm using the following version of gcc:
>
> gcc version 2.95.3 20010315 (release) (NetBSD nb3)
>
> Thank you.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Segmentation fault Causes
  2003-02-24  5:39 Segmentation fault Causes Ajay Bansal
@ 2003-02-24  8:00 ` Sergei
  0 siblings, 0 replies; 7+ messages in thread
From: Sergei @ 2003-02-24  8:00 UTC (permalink / raw)
  To: gcc-help

On Mon, 24 Feb 2003 11:06:35 +0530
"Ajay Bansal" <Ajay_Bansal@infosys.com> wrote:

> John
> 
> Can you please tell me how to use this fprintf(stderr... . ) function?
> 
> Do I need to give it at many different places in the code??? If that is
> the case, what's is the difference between putting many different
> "cerr<<................"???

`fprintf' is from standard C library. `cerr <<' is from C++.

You may learn more about fprintf from `man fprintf'. Do #include <stdio.h> if you want to use it in your code.

Best regards,
Sergei

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Segmentation fault Causes
  2003-02-24  5:07 Daniel Bolgheroni
                   ` (2 preceding siblings ...)
  2003-04-22  7:47 ` bjorn rohde jensen
@ 2003-04-22 18:24 ` Dikandé Alain Moïse
  3 siblings, 0 replies; 7+ messages in thread
From: Dikandé Alain Moïse @ 2003-04-22 18:24 UTC (permalink / raw)
  To: Daniel Bolgheroni; +Cc: gcc-help

.. Most of core dumping signals indicate bad pointer adresses to your outputs/inputs. Check that your program is not trying to access a memory location 
outside its address.  Bad use of pointers cause bus errors and segmentation violations are typically
out-of-bounds array references, and/or references through uninitialized or
mangled pointers.  Look very closely in your program for bizarre things like
that.  A frequent example of bad use of pointers adress in C is:

        int b;
        scanf("%d", b);

When compiled, you will have the "segmentation fault" signal. Instead, with the correct version:

        int c;
        scanf("%d", &c);

No "segmentation fault message(core file) is given(generated). 

In  C++, a typical example is:

        int* q=new int[150];
        cout<< q[100];

instead of the correct version:

        int* Q=new int[200];
        cout << Q[199]

Good Kuck..

En réponse à Daniel Bolgheroni <dab__@uol.com.br>:

> What are the causes that a program can give an error "Segmentation fault
> (core dumped)"?
> 
> I'm compiling a program I'm writing but I getting some of this error
> messages executing it. I looked into gcc FAQ and gcc 2.95 manual, but I
> found no references to this problem.
> 
> I'm using the following version of gcc:
> 
> gcc version 2.95.3 20010315 (release) (NetBSD nb3)
> 
> Thank you.
> 
> 
> 



                      DIKANDÉ ALAIN MOÏSE
Present: CERPEMA, Département de Physique, fac. Sciences, Univ. Sherbrooke J1k-2R1 
Québec, Canada. tel: (819)821-8000 ext. 3043.
Permanent: Département de Physique, Faculté des Sciences, Université de Douala BP 24157 Douala, Cameroun.

"Le mépris des autres et des choses, plutôt que nous grandir, nous rabaisse et 
met en péril notre capacité de compréhension du monde et de l' humanité". 
Albert Einstein.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Segmentation fault Causes
  2003-02-24  5:07 Daniel Bolgheroni
  2003-02-24  5:22 ` J.C.Wren
  2003-04-22  1:02 ` Daniel Bolgheroni
@ 2003-04-22  7:47 ` bjorn rohde jensen
  2003-04-22 18:24 ` Dikandé Alain Moïse
  3 siblings, 0 replies; 7+ messages in thread
From: bjorn rohde jensen @ 2003-04-22  7:47 UTC (permalink / raw)
  To: Daniel Bolgheroni; +Cc: gcc-help

Hi there,

  Segmentation faults are signals delivered from the operating
system to a process to inform it, that the process has attempted
to access memory outside its address space. Common causes are
forgetting to initialise a pointer before use or using something,
which is not a pointer, as a pointer.
  You might want to compile with debug info and run the program
under a debug'er. It should provide you with the exact line,
where the segfault is generated. If you are fairly lucky, the
reason will be also be quite apparent.

Yours sincerely,

Bjorn

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Segmentation fault Causes
  2003-02-24  5:07 Daniel Bolgheroni
  2003-02-24  5:22 ` J.C.Wren
@ 2003-04-22  1:02 ` Daniel Bolgheroni
  2003-04-22  7:47 ` bjorn rohde jensen
  2003-04-22 18:24 ` Dikandé Alain Moïse
  3 siblings, 0 replies; 7+ messages in thread
From: Daniel Bolgheroni @ 2003-04-22  1:02 UTC (permalink / raw)
  To: gcc-help

What are the causes that a program can give an error "Segmentation fault (core dumped)"?

I'm compiling a program I'm writing but I getting some of this error messages executing it. I looked into gcc FAQ and gcc 2.95 manual, but I found no references to this problem.

I'm using the following version of gcc:

gcc version 2.95.3 20010315 (release) (NetBSD nb3)

Thank you.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Segmentation fault Causes
  2003-02-24  5:07 Daniel Bolgheroni
@ 2003-02-24  5:22 ` J.C.Wren
  2003-04-22  1:02 ` Daniel Bolgheroni
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: J.C.Wren @ 2003-02-24  5:22 UTC (permalink / raw)
  To: 'Daniel Bolgheroni', gcc-help

	Most commonly it's caused by trying to write to a pointer that either is
uninitialized, or NULL.

int main (int argc, char **argv)
{
   char *p = NULL;
   *p = 'A';
}

will cause a segfault.  Also, if 'p' isn't assigned an explicit value, it
will have whatevers on the stack at that point.  Another thing that will
cause a segfault is writing passed the end of an array:

int main (int argc, char **argv)
{
   char buffer [16];
   memset (buffer, 0, 64);
}

will segfault.

	I would suggest compiling your program with the -W and -Wall switches
('gcc -W -Wall myprog.c -o myprog').  Fix any and all warnings.  GCC is very
good about warning about usage of uninitialized variables.

	If you want to see how far you're getting in your program use 'fprintf
(stderr, "Got to line %d\n", __LINE__);'.  This will show how far you've got
before the segfault occurs.  You must write to stderr and not stdout because
of buffering.  When the program segfaults, unbuffered I/O will NOT be
written to the console.  This is why people often make the mistake of saying
'printf ("Got to line %d\n", __LINE__);' and thinking they really didn't get
that far.  In reality, it did get that far, but the I/O was lost when the
buffer remained unflushed after the segfault.  There are some exceptions to
this rule, but it's not worth pursuing at this point (just so someone does
say "but if you set unbuffered I/O mode, that doesn't apply!").

	--John


> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org]On
> Behalf Of Daniel Bolgheroni
> Sent: Sunday, February 23, 2003 23:55
> To: gcc-help@gcc.gnu.org
> Subject: Segmentation fault Causes
>
>
> What are the causes that a program can give an error
> "Segmentation fault (core dumped)"?
>
> I'm compiling a program I'm writing but I getting some of
> this error messages executing it. I looked into gcc FAQ and
> gcc 2.95 manual, but I found no references to this problem.
>
> I'm using the following version of gcc:
>
> gcc version 2.95.3 20010315 (release) (NetBSD nb3)
>
> Thank you.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Segmentation fault Causes
@ 2003-02-24  5:07 Daniel Bolgheroni
  2003-02-24  5:22 ` J.C.Wren
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Daniel Bolgheroni @ 2003-02-24  5:07 UTC (permalink / raw)
  To: gcc-help

What are the causes that a program can give an error "Segmentation fault (core dumped)"?

I'm compiling a program I'm writing but I getting some of this error messages executing it. I looked into gcc FAQ and gcc 2.95 manual, but I found no references to this problem.

I'm using the following version of gcc:

gcc version 2.95.3 20010315 (release) (NetBSD nb3)

Thank you.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-04-22 18:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-24  5:39 Segmentation fault Causes Ajay Bansal
2003-02-24  8:00 ` Sergei
  -- strict thread matches above, loose matches on Subject: below --
2003-02-24  5:07 Daniel Bolgheroni
2003-02-24  5:22 ` J.C.Wren
2003-04-22  1:02 ` Daniel Bolgheroni
2003-04-22  7:47 ` bjorn rohde jensen
2003-04-22 18:24 ` Dikandé Alain Moïse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).