From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9464 invoked by alias); 24 Feb 2003 05:39:08 -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 9457 invoked from network); 24 Feb 2003 05:39:08 -0000 Received: from unknown (HELO bosvwl01.infosys.com) (216.52.49.35) by 172.16.49.205 with SMTP; 24 Feb 2003 05:39:08 -0000 Received: from 192.168.200.81 by bosvwl01.infosys.com (InterScan E-Mail VirusWall NT); Mon, 24 Feb 2003 00:27:07 -0500 Received: from mohmsg01.ad.infosys.com ([192.168.122.42]) by indhubbhs01.ad.infosys.com with Microsoft SMTPSVC(5.0.2195.5329); Mon, 24 Feb 2003 11:12:12 +0530 content-class: urn:content-classes:message Subject: RE: Segmentation fault Causes MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Date: Mon, 24 Feb 2003 05:39:00 -0000 X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 Message-ID: <2B721C6525F0D411B1E900B0D0226BDD02148F82@mohmsg01.ad.infosys.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: From: "Ajay Bansal" To: X-OriginalArrivalTime: 24 Feb 2003 05:42:12.0381 (UTC) FILETIME=[7ABA74D0:01C2DBC7] X-SW-Source: 2003-02/txt/msg00222.txt.bz2 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]=20 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 =3D NULL; *p =3D '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=20 > fault (core dumped)"? > > I'm compiling a program I'm writing but I getting some of this error=20 > messages executing it. I looked into gcc FAQ and gcc 2.95 manual, but=20 > 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.