Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #99686

Re: [File Handling] fread(), fseek() and text file

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: [File Handling] fread(), fseek() and text file
Date 2016-12-16 12:46 -0800
Organization None to speak of
Message-ID <lna8bvk3fn.fsf@kst-u.example.com> (permalink)
References (8 earlier) <f2a424b3-1a56-4c49-b792-ed9fcd9969e9@googlegroups.com> <o318om$m6s$1@dont-email.me> <bb0da670-4df4-4db4-a023-bac9f68b039d@googlegroups.com> <o31cc7$4gs$1@dont-email.me> <8737hn1vg3.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Richard Heathfield <rjh@cpax.org.uk> writes:
>> On 16/12/16 18:03, Dexterr wrote:
>>> 	// Memory allocation for struct Components
>>> 	Components *series_connection = (Components*) malloc(sizeof
>>> *series_connection);
>>
>> For the last time: you don't need the cast.
>>
>> Components *series_connection = malloc(sizeof *series_connection);
>>
>> (If you can't learn, I can't teach you.)
>
> It's possible the OP has been told to use a cast, or, at least it's been
> there in examples in class or in the course materials.  Would you rather
> do it the way the person marking your paper does it, or the way someone
> on Usenet told you to do it? :-)
>
> <snip>

It could also be helpful to explain *why* the cast shouldn't be there.
This has been explained many times, but perhaps not in this thread.
Feel free to stop reading if you already know this stuff.

malloc() returns a pointer result of type void*, which is more or less a
generic pointer type.  C permits a value of type void* to be implicitly
converted (via assignment, initialization, etc.) to any
pointer-to-object type, without the need for a cast.

That's why you don't *need* the cast, but it doesn't explain why you
*shouldn't* use the cast.

A cast can in effect override type checking.  The syntax to convert from
one numeric type to another (which is usually a safe operation) is the
same as the syntax to convert from one pointer type to another, or from
an integer to a pointer or vice versa (such conversions can be very
dangerous).  After decades of experience, most C programmers have
concluded that minimizing casts, and depending as much as possible on
implicit conversions, is the safest approach.  It means that any time
there is a cast, it should be viewed with some suspicion.

If you write:

    some_type *pointer = (some_type*)malloc(sizeof *pointer);

then you're specifying the same conversion that would have been done
implicitly without the cast.  But you're also writing the type name
twice.  The reader has to verify that the type in the cast matches the
type of the pointer, and that the conversion is appropriate.

In earlier versions of C (C90 and earlier), it was legal to call a
function with no visible declaration; such a function was *assumed* to
return an int result.  So if you forgot the "#include <stdlib.h>" that
provides the declaration of malloc() *and* omitted the cast, the
compiler would probably warn you about an implicit int-to-pointer
conversion.  If you forgot the #include and added the cast, the cast
would specify a conversion from int to some_type* rather than from void*
to some_type* -- and the compiler would not complain about it; by using
a cast, you promise the compiler that you know what the heck you're
doing.  That consideration *mostly* doesn't apply anymore, but some
compilers still default to C90 rules.

This is, not surprisingly, a frequently asked question, covered in
http://www.c-faq.com/ questions 7.6 and following.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 01:10 -0800
  Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 09:59 +0000
    Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 05:28 -0800
      Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 14:02 +0000
        Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 06:30 -0800
          Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 15:20 +0000
            Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 08:54 -0800
              Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 17:00 +0000
                Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 09:32 -0800
                Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 17:38 +0000
                Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-16 10:03 -0800
                Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-16 18:40 +0000
                Re: [File Handling] fread(), fseek() and text file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-16 20:16 +0000
                Re: [File Handling] fread(), fseek() and text file Keith Thompson <kst-u@mib.org> - 2016-12-16 12:46 -0800
                Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 05:47 -0800
                Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 05:49 -0800
                Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 05:43 -0800
                Re: [File Handling] fread(), fseek() and text file Barry Schwarz <schwarzb@dqel.com> - 2016-12-17 08:48 -0800
                Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 12:03 -0800
                Re: [File Handling] fread(), fseek() and text file Keith Thompson <kst-u@mib.org> - 2016-12-17 12:22 -0800
                Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 12:23 -0800
  Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-17 12:26 -0800
    Re: [File Handling] fread(), fseek() and text file asetofsymbols@gmail.com - 2016-12-18 12:41 -0800
      Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-18 13:04 -0800
      Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-18 13:08 -0800
        Re: [File Handling] fread(), fseek() and text file asetofsymbols@gmail.com - 2016-12-19 06:14 -0800
          Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-19 10:38 -0800
            Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-19 18:51 +0000
              Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-19 11:14 -0800
                Re: [File Handling] fread(), fseek() and text file Richard Heathfield <rjh@cpax.org.uk> - 2016-12-19 19:35 +0000
          Re: [File Handling] fread(), fseek() and text file Dexterr <dejangru@gmail.com> - 2016-12-19 10:43 -0800

csiph-web