Re: I was told this line of code would get me fired
- From: "Chad" <cdalten@xxxxxxxxx>
- Date: 13 Mar 2006 19:58:48 -0800
Chuck Dillon wrote:
toby wrote:
Chad wrote:
Given the following lines of code:
...snip...
I was told the follwing line of code:
for(;fread(&log_file,sizeof(struct utmp),1,fp) !=0;)
would have gotten me fired had I used this out in the workforce. Can
someone clue me on what is going wrong here? Just for the record, I'm
not a computer programmer.
Others better than I will complete the prosecution's arguments, but
even a casual inspection tells me that on any error in fread(), the
loop will not exit (likely to spin endlessly); the error code is
discarded; and short reads are likewise not detected.
My fread usage is a bit stale but I think you are thinking of read(2).
From the fread/fwrite manpage (SunOS 5.8)...
<Paste>
RETURN VALUES
The fread() function returns the number of items read. The
fwrite() function returns the number of items written.
If size or nitems is 0, then fread() and fwrite() return 0
and do not effect the state of stream.
If an error occurs, fread() and fwrite() return 0 and set
the error indicator for stream.
</Paste>
So why would it get into an endless loop? Since the item count is 1
the conditional is equivalent to 'while (fread(...) == 1)' which is the
same as 'while (I read a record without error)'.
-- ced
Why would it go into an endless loop? I have no idea. Okay, I need
think think about how the item count is one.
If you're not a programmer, why would you care? Are you the boss of the
guy who wrote this? :)
Thanks in advance
Chad
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
.
- Follow-Ups:
- References:
- I was told this line of code would get me fired
- From: Chad
- Re: I was told this line of code would get me fired
- From: toby
- Re: I was told this line of code would get me fired
- From: Chuck Dillon
- I was told this line of code would get me fired
- Prev by Date: Re: Unicode version of main in Unix
- Next by Date: Re: I was told this line of code would get me fired
- Previous by thread: Re: I was told this line of code would get me fired
- Next by thread: Re: I was told this line of code would get me fired
- Index(es):
Relevant Pages
|