Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: sizeof object got by dereferencing uniniitalized pointer Date: Thu, 28 Nov 2019 14:55:23 -0800 Organization: None to speak of Lines: 37 Message-ID: References: <21e377ab-2a49-4f7e-bc18-72f1cee63204@googlegroups.com> <9d5ee6d4-607b-405f-83d9-afbe2b073cac@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="51024748ba335f5128a925ed3f1a83c2"; logging-data="22705"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MAuccp0EQqe3SWKik9VH1" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:BPrL2ZDqW9nfYAFhBQNoPu5Kw28= sha1:edxuYVvD3myD78W2bKLo/caoTZg= Xref: csiph.com comp.lang.c:148200 Malcolm McLean writes: [...] > The sizeof keyword looks like a function, but it is in fact a built-in > operator. You are not really derefencing the pointer when you pass it *p, > you are telling it, at compile time, to take the size of the type p > points to. sizeof is an operator. sizeof(expr) only looks like a function call because of the unnecessary parentheses. Similarly, return(expr); looks like a function call because it's written that way. Which is exactly why I write `sizeof expr` and `return expr;`. (And `while (condition)` rather than `while(condition)`.) sizeof is a special case in C because it's the only operator (other than _Alignof, but that doesn't take an expression as its operand) whose symbol is a keyword rather than punctuation, and keywords look like identifiers. If C had used $ as the symbol instead, there wouldn't be any temptation to write `$(expr)`; instead, we'd just write `$ expr` or even `$expr` (unless parentheses are needed for disambiguation). > This is undesireable, and my view is that the form sizeof(*p) should be > avoided. Some people recommend it because the code won't break if the > type p points to changes. However you will certainly need to review every > use of p in this case anyway, so I stick to my advice. Just use `sizeof *p`. Understanding how sizeof works should be a prerequisite to writing code that uses it. (And that includes being able to read either `sizeof *p` or `sizeof(*p)` without confusion.) You need to distinguish clearly between identifiers and keywords. (I wouldn't write `sizeof*p`, though.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */