Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #153664
| From | John Forkosh <forkosh@panix.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide |
| Date | 2020-08-16 05:47 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <rhaha5$hpi$1@reader1.panix.com> (permalink) |
| References | (6 earlier) <87k0y312ua.fsf@nosuchdomain.example.com> <rh2nh6$gv4$1@reader1.panix.com> <slrnrjgpo7.2as.please@logancomp.rathbonelaw.com> <1p_ZG.346740$PN2.51215@fx48.iad> <87o8nb9zdd.fsf@bsb.me.uk> |
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> Richard Damon <Richard@Damon-Family.org> writes:
>> Poprocks wrote:
>>> John Forkosh wrote:
>>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>>> John Forkosh <forkosh@panix.com> writes:
>>>>> [...]
>>>>>> I personally do bind the * to the *p_, but would also write it as,
>>>>>> typedef struct Meow meow;
>>>>>> meow *p_cat = (meow *)malloc(sizeof(meow));
>>>>>> which is just how I personally like to read and write it.
>>>>>> And if that faq doesn't like it, then it can just go rewrite itself:)
>>>>>
>>>>> OK, why do you like the cast? What is the benefit of it? Why do you
>>>>> think having the cast is better than not having it?
>>>>>
>>>>> You are of course entitled to your opinion, I'm just wondering if
>>>>> you have a basis for it.
>>>>
>>>> Basically, just opinion. Actually, not even rising to the level
>>>> of "opinion". I just prefer each term to be explicit in-and-of itself,
>>>> without needing the entire statement's context to determine its
>>>> semantics. Just my preference.
>>>>
>>>> But this particular bit of faq -pedanticism wasn't so bothersome all
>>>> by itself. Rather, the overcontrol of programming style it suggests.
>>>> I originally asked my followup question because I wondered if
>>>> there's some actual potential problem/pitfall I was unaware of.
>>>> So it was kind of annoying to just see some style control freak
>>>> telling me how to write C.
>>>
>>> I've been reviewing this thread with interest, and I *do* hear what
>>> you're saying.
>>>
>>> I think when I originally started learning C (and I am still *very* much
>>> in the learning process, mind you -- so people should take what I say
>>> with a *very* large grain of salt) I looked at the prototype of the
>>> stdlib `malloc' function and thought, "well, it returns a `void *', so I
>>> should cast that return value to the actual type I intend to utilize,
>>> because I don't want a `void *', I want a `char *' (or a `struct foo *'
>>> or what-have-you)," without being fully cognizant of how this is not
>>> necessary, because implicit conversions are done.
>>>
>>> So, knowing that it is both (a) not necessary, and (b) *may* cause
>>> harmful unexpected effects, I have changed any of my prior practices and
>>> code that involved casting the return of malloc. My thinking is, what
>>> is the point of doing something that is both (a) and (b) above, with no
>>> tangible benefit other than to make my code look "better" in some way
>>> by adding these casts.
>>>
>>> I can understand where mere "opinion" (as in, "it's just my
>>> opinion/style, it's no better/worse than any other") comes into play
>>> when it comes to mere aesthetic style; eg, tabs vs. spaces, placement of
>>> curly braces, etc., but where the opinion may cause detrimental effects
>>> without any actual upside, I don't get it.
>>>
>> As far as I know, the only two reasons I have every heard for why you
>> don't want the cast are:
>>
>> 1) (From the FAQ) If you forgot to include <stdlib,h> the default
>> signature for the function of returning an int will be caught by not
>> casting, but will be hid by casting. This reason went out in C99 when
>> the call with a declaration went away.
>>
>> 2) From a stylistic point of view, this is a redundant statement of
>> type, and some people like to remove that redundancy. I get that,
>> which is why I tend to use a macro...
>>
>> #define NEW(T) (T*)malloc(sizeof(T))
>> #define NEW_ARRAY(T, n) (T*)malloc((n)*sizeof(T))
>
> Or, to remove even more redundancy,
> #define NEW_ARRAY(T, n) ((T*)malloc((n)*sizeof(T)))
> #define NEW(T) NEW_ARRAY(T, 1)
Redundancy aside, that n arg is why I originally posted a
followup question, asking about calloc() rather than malloc().
Although I do typically write
int n = 99; /* or whatever */
typedef Whatever whatever;
whatever *p = (whatever *)malloc(n*sizeof(whatever));
there is (or at least there used to be) some potential
boundary alignment problem, whereby calloc(n,sizeof(whatever))
might be potentially safer. So is boundary alignment never
a problem anymore, as of some more recent C, or what?
(That's really the only question/answer I was looking for when
posting. Sorry about all the extraneous jibber-jabber followup:)
> (This has the minor pedagogic side effect of reminding the reader that
> an object behaves like and array of length one.)
>
>> This also eliminates the chance of getting the two different by mistake
>
> But (a detail) it has problems when T is an array type -- unless you use
> a typedef name.
--
John Forkosh ( mailto: j@f.com where j=john and f=forkosh )
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
"Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Lynn McGuire <lynnmcguire5@gmail.com> - 2020-08-12 13:54 -0500
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-12 12:23 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Elijah Stone <elronnd@elronnd.net> - 2020-08-12 15:05 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Damon <Richard@Damon-Family.org> - 2020-08-12 19:15 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Philipp Klaus Krause <pkk@spth.de> - 2020-08-13 10:36 +0200
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Poprocks <please@replytogroup.com> - 2020-08-13 14:23 +0000
What do when -Werror gets in your way (Was: "Why the C Language Will Never Stop You from Making Mistakes") by JeanHeyd Meneide gazelle@shell.xmission.com (Kenny McCormack) - 2020-08-13 14:49 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Elijah Stone <elronnd@elronnd.net> - 2020-08-13 23:25 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide John Forkosh <forkosh@panix.com> - 2020-08-13 01:15 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-12 18:42 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide John Forkosh <forkosh@panix.com> - 2020-08-13 06:15 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-12 23:23 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide John Forkosh <forkosh@panix.com> - 2020-08-13 06:44 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Poprocks <please@replytogroup.com> - 2020-08-15 22:51 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Damon <Richard@Damon-Family.org> - 2020-08-15 19:41 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-08-16 02:02 +0100
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide John Forkosh <forkosh@panix.com> - 2020-08-16 05:47 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-08-16 12:03 +0100
Kinda by definition... (Was: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide) gazelle@shell.xmission.com (Kenny McCormack) - 2020-08-16 11:49 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-16 05:33 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide John Forkosh <forkosh@panix.com> - 2020-08-17 06:27 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Damon <Richard@Damon-Family.org> - 2020-08-16 09:12 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Damon <Richard@Damon-Family.org> - 2020-08-13 07:38 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-13 09:33 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Damon <Richard@Damon-Family.org> - 2020-08-14 10:50 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-14 12:39 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Damon <Richard@Damon-Family.org> - 2020-08-14 16:53 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-14 19:49 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-08-15 02:31 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-15 16:10 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Damon <Richard@Damon-Family.org> - 2020-08-16 09:25 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-16 10:50 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-16 15:40 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Damon <Richard@Damon-Family.org> - 2020-08-16 19:39 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-16 21:12 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Harnden <richard.nospam@gmail.com> - 2020-08-18 08:47 +0100
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-18 08:26 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-28 08:10 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide gazelle@shell.xmission.com (Kenny McCormack) - 2020-08-13 12:48 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide John Bode <jfbode1029@gmail.com> - 2020-08-13 09:46 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-08-13 06:14 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Anton Shepelev <anton.txt@gmail.com> - 2020-08-13 01:19 +0300
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-12 16:18 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Scott Newman <scott69@gmail.com> - 2020-08-13 09:25 +0200
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-13 00:41 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Scott Newman <scott69@gmail.com> - 2020-08-13 12:53 +0200
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Richard Harnden <richard.nospam@gmail.com> - 2020-08-13 12:52 +0100
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide David Brown <david.brown@hesbynett.no> - 2020-08-13 15:17 +0200
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-13 10:38 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Scott Newman <scott69@gmail.com> - 2020-08-13 19:44 +0200
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Bart <bc@freeuk.com> - 2020-08-13 19:10 +0100
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-08-13 11:42 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide scott@slp53.sl.home (Scott Lurndal) - 2020-08-14 05:56 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Bonita Montero <Bonita.Montero@gmail.com> - 2020-08-14 09:14 +0200
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-14 08:45 -0400
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide gazelle@shell.xmission.com (Kenny McCormack) - 2020-08-14 16:51 +0000
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Ian Collins <ian-news@hotmail.com> - 2020-08-15 09:11 +1200
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-24 16:58 -0700
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Philipp Klaus Krause <pkk@spth.de> - 2020-08-13 10:32 +0200
Re: "Why the C Language Will Never Stop You from Making Mistakes" by JeanHeyd Meneide Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-23 07:04 -0700
csiph-web