public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "J.C.Wren" <jcwren@jcwren.com>
To: "'Daniel Bolgheroni'" <dab__@uol.com.br>, <gcc-help@gcc.gnu.org>
Subject: RE: Segmentation fault Causes
Date: Mon, 24 Feb 2003 05:22:00 -0000	[thread overview]
Message-ID: <003001c2dbc4$b6e96fe0$020010ac@k4jcw> (raw)
In-Reply-To: <20030224045442.GA25932@uol.com.br>

	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.


  reply	other threads:[~2003-02-24  5:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-24  5:07 Daniel Bolgheroni
2003-02-24  5:22 ` J.C.Wren [this message]
2003-04-22  1:02 ` Daniel Bolgheroni
2003-04-22  7:47 ` bjorn rohde jensen
2003-04-22 18:24 ` Dikandé Alain Moïse
2003-05-24 11:07 ` RedHat 8.0 `istream_iterator' undeclared (first use this function) MR
2003-05-24 12:21   ` Oscar Fuentes
2003-05-24 12:50   ` LLeweLLyn Reese
2003-02-24  5:39 Segmentation fault Causes Ajay Bansal
2003-02-24  8:00 ` Sergei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='003001c2dbc4$b6e96fe0$020010ac@k4jcw' \
    --to=jcwren@jcwren.com \
    --cc=dab__@uol.com.br \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).