Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: A glitch regarding the usual malloc() idiom Date: Thu, 21 Jul 2016 09:44:11 -0700 Organization: None to speak of Lines: 66 Message-ID: References: <4cd22745-22ad-4ac7-bdba-39ffa1c75e13@googlegroups.com> <48bfec8e-1f8b-4a7d-9de9-2f4b203efa88@googlegroups.com> <706cdd84-add9-4c8d-8210-10a5cf8e5b25@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="20568"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sN2BzbnYCO2MDUE8C3hYq" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:mu39TLP4946ULkBcEHMcImZWPiU= sha1:DO0jsMe3trRl8AYVtPuDmu/kAOI= Xref: csiph.com comp.lang.c:90725 BartC writes: > On 21/07/2016 03:57, Keith Thompson wrote: >> BartC writes: >> [...] >>> I think if you have a problem with writing 'sizeof(*p)' in the interests >>> of unambiguity, which is actually only one character more than 'sizeof >>> *p', then perhaps C isn't the right language for you! >> >> `sizeof *p` is not ambiguous, so adding parentheses "in the interests of >> unambiguity" doesn't make sense. > > Neither is 'a*sizeof*p' ambiguous according to the language, yet people > tend to write: 'a*sizeof *p'; why is that? > > Neither is this example that someone posted: > > sizeof (long)-2 > > Not according to the language. But to me it looks confusing. Sure, if you deliberately use whitespace in a way that conflicts with the meaning of the expression. See also `x+y * z`. > (long) can be the parenthesised type needed by sizeof, or it can be a > cast at the start of a single-term non-parenthesised expression. > Presumably some detail of the grammar tells you which is matched first**. > > But, if parentheses were obligatory, or you knew the style always used > them, then you'd know immediately the above would *always* appear as: > > sizeof (long)-2 // meaning (sizeof (long))-2 > sizeof ((long)-2) > > You'd know /for sure/ that the sizeof operand ended at the first > parenthesis matching the opening one. Of course there are cases where adding parentheses, even if they aren't strictly necessary, can make an expression clearer. `sizeof (long) - 2` may be one of those cases. We simply disagree on whether `sizeof *p` is another -- and I've wasted too much time arguing the point with you. > (** The expression that follows sizeof is a single term (not a general > expression unless it's parenthesised), however that single term is split > into two kinds: > > * Those that start with a cast > * All other terms that don't start with a cast No, the parenthesized type name in `sizeof ( type-name )` is not a cast, any more than the commas in a function call are comma operators. > Normal unary ops can use either. But the one following sizeof can only > be without a cast. (So a bodge IMO.) > > Not everyone writing or reading C will be conversant with its formal > grammar, so I say that that sizeof (long)-2 example /looks/ ambiguous.) Strawman argument. It looks ambiguous because you deliberately used deceptive whitespace to make it look ambiguous. -- Keith Thompson (The_Other_Keith) kst-u@mib.org 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"