delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/11/01/10:15:58

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: Lewis Hyatt <lhyatt AT princeton DOT edu>
Subject: Re: can't read sequential files
Date: Thu, 01 Nov 2007 11:15:28 -0400
Lines: 45
Message-ID: <fgcqid$nj5$1@ger.gmane.org>
References: <13500536 DOT post AT talk DOT nabble DOT com> <13524102 DOT post AT talk DOT nabble DOT com> <005c01c81c79$d7545d30$2e08a8c0 AT CAM DOT ARTIMI DOT COM>
Mime-Version: 1.0
User-Agent: Thunderbird 2.0.0.6 (Windows/20070728)
In-Reply-To: <005c01c81c79$d7545d30$2e08a8c0@CAM.ARTIMI.COM>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Dave Korn wrote:
> On 01 November 2007 06:43, zirtik wrote:
> 
>> After adding the line:
>>
>>
>> 	if (fp==NULL)
>> 	{
>> 	   printf("error, NULL pointer!\n");
>> 	   return(1);
>> 	}
>>
>> and then rebuilding the code, everything worked. But it's strange that if I
>> delete that code segment and never check whether the fp pointer is NULL or
>> not, I always get a segmentation fault. Can this be some kind of an
>> optimization problem? I don't know why it happens. Thank you for the
>> comments.
> 
>   A NULL pointer is never valid in C, and a segfault is what you get if you
> try to make use of one (by dereferencing it).  You get a NULL pointer back
> from fopen when it fails; a lot of library routines do this to indicate
> failure, because any other value could be a valid pointer.  The other library
> routines, such as fread and fwrite, will assume that you have done your error
> checking and won't be passing them a NULL pointer, so they won't bother to
> check what file pointer you pass them, they'll just go ahead and try and use
> it.  So if you get a NULL pointer back from fopen and you don't check for it,
> your code carries on and passes that same pointer to fread, which tries to use
> it as if it pointed to a real FILE object, and crashes.
> 
>   The comp.lang.c FAQ has an entire section on NULL pointer, section 5.  It
> should be available at
> http://c-faq.com/null/index.html
> but the website seems to be temporarily down right now; there's also a copy at
> faqs.org:
> http://www.faqs.org/faqs/C-faq/faq/

I think what the OP is saying is that if he adds the check for null, 
then his code works normally, including the file read operation, (ie, 
the pointer is not null), but if he removes the check, then he gets a 
segfault. Strange behavior like this is typically the result of memory 
corruption from earlier in the program. If you can post your entire code 
(preferably a minimal example that exhibits the behavior), then we can 
probably locate the problem.

-Lewis


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019