From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1853 invoked by alias); 24 Feb 2003 05:22:26 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 1717 invoked from network); 24 Feb 2003 05:22:26 -0000 Received: from unknown (HELO imf18bis.bellsouth.net) (205.152.58.38) by 172.16.49.205 with SMTP; 24 Feb 2003 05:22:26 -0000 Received: from k4jcw ([65.80.50.145]) by imf18bis.bellsouth.net (InterMail vM.5.01.04.25 201-253-122-122-125-20020815) with SMTP id <20030224052425.YUZN1178.imf18bis.bellsouth.net@k4jcw>; Mon, 24 Feb 2003 00:24:25 -0500 From: "J.C.Wren" To: "'Daniel Bolgheroni'" , Subject: RE: Segmentation fault Causes Date: Mon, 24 Feb 2003 05:22:00 -0000 Message-ID: <003001c2dbc4$b6e96fe0$020010ac@k4jcw> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal In-Reply-To: <20030224045442.GA25932@uol.com.br> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Importance: Normal X-SW-Source: 2003-02/txt/msg00220.txt.bz2 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.