Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #148294
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: sizeof object got by dereferencing uniniitalized pointer |
| Date | Fri, 29 Nov 2019 07:09:16 -0800 |
| Organization | A noiseless patient Spider |
| Lines | 106 |
| Message-ID | <86o8wu7m8z.fsf@linuxsc.com> (permalink) |
| References | <21e377ab-2a49-4f7e-bc18-72f1cee63204@googlegroups.com> <9d5ee6d4-607b-405f-83d9-afbe2b073cac@googlegroups.com> <76a74489-4a1f-4561-a01b-b2962a10bae4@googlegroups.com> <qromtr$3o3$1@dont-email.me> <2119c5dc-fc01-4a74-93f4-8027c7cc210e@googlegroups.com> <c97a16bb-4384-5e83-2c2f-1099b62be653@hrosenau.de> <43093894-ed52-47de-85f4-eadd92a1499e@googlegroups.com> <877e3j52do.fsf@bsb.me.uk> <86eexr8qlh.fsf@linuxsc.com> <87v9r33211.fsf@bsb.me.uk> <861rtr8ndf.fsf@linuxsc.com> <Hb%DF.30271$s93.29962@fx19.iad> <lnlfrzulg0.fsf@kst-u.example.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | reader02.eternal-september.org; posting-host="81b92e3fa8397acac1ab5cf195ff75cd"; logging-data="24025"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+P7FDfdhSUFL0PvPWlDsNhZPMx9W6PiGg=" |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) |
| Cancel-Lock | sha1:KF+Qvsa9KO7Ec70T70b+KKauuj4= sha1:wBV6LxwSZIvaN5/Q21WRWKJIIt0= |
| Xref | csiph.com comp.lang.c:148294 |
Show key headers only | View raw
Keith Thompson <kst-u@mib.org> writes:
> Richard Damon <Richard@Damon-Family.org> writes:
> [...]
>
>> I had always read the phrase from 6.5.3.4p2
>>
>> The sizeof operator yields the size (in bytes) of its operand, which may
>> be an expression or the parenthesized name of a type. The size is
>> determined from the type of the operand. The result is an integer. If
>> the type of the operand is a variable length array type, the operand is
>> evaluated; otherwise, the operand is not evaluated and the result is an
>> integer constant.
>>
>> As referring to if the operand WAS a variable length array type, but the
>> wording doesn't quite say that. My guess is that is what was intended.
>
> I *think* you're saying that the "operand is evaluated" clause is meant
> to apply to `sizeof (vla_type)` but not to `sizeof vla_object`. Is that
> what you meant?
That is how I took his comment.
> I don't agree that that's what the wording in the
> standard says, but I do agree that it could be a more reasonable rule.
>
> Off the top of my head, I can't think of a case where `sizeof expr`,
> where expr is of VLA type, actually needs to evaluate expr to
> determine its size. (I'd be interested in seeing an example.)
> But the standard seems to require it to be evaluated anyway, and
> most compilers follow that.
>
> An example:
>
> #include <stdio.h>
>
> int ident(int n) {
> printf("ident(%d)\n", n);
> return n;
> }
>
> int main(void) {
> int dim = 42;
> int vla[dim][dim];
> printf("sizeof vla[ident(10)] = %zu\n", sizeof vla[ident(10)]);
> }
>
> The size can be determined without calling ident(10), but gcc,
> clang, and icc (correctly following the standard, I think) generate
> a call, i.e., they evaluate (part of) the operand of the sizeof.
> (tcc doesn't, but I don't expect tcc to be fully conforming.)
>
> A painfully literal reading of "the operand is evaluated" could
> mean that it has to evaluate the value of the array object vla[10],
> which would mean evaluating the value of each of its 42 elements.
> That's clearly unnecessary, and surely was not the intent.
The Standard specifically excludes that possibility, in section
6.3.2.1 p2.
Except when it is the operand of the sizeof operator [or
other exceptions], an lvalue that does not have array type
is converted to the value stored in the designated object
(and is no longer an lvalue) [...]
> In general `sizeof vla_expression` requires evaluating *something* at
> run time, typically retrieving the value of an anonymous object that
> holds the array length or size, and possibly doing some arithmetic on
> it. But that doesn't evaluate, or require evaluating, the operand.
The Standard says it does evaluate the operand.
Moreover there are cases where evaluating the operand is necessary
to determine the size:
int foo = sizeof *(int (*)[ ident( 1 ) ]){ &something };
This expression needs to evaluate the sizeof operand expression
to determine the value to assign to foo.
> On the other hand, if the operand is a parenthesized type name,
> the standard doesn't tell us what "evaluating" a type name means.
In a sense it does, in 6.7.6.2 p5, talking about array declarators.
That section concerns declaring variables, but a type name is like a
variable declaration except it omits the variable identifier. It
seems reasonable to infer that type names are evaluated like their
variable declarator counterparts.
> Only expressions are evaluated. It could mean evaluating
> any expressions contained within the type name (as in `sizeof
> (int[func()])` -- note that evaluating `func()` is actually
> necessary to determine the size). I think the standard could be
> improved by making that clearer.
It's true that only expressions are evaluated, but any type name
that is part of an expression is an operand in the expression
and so must be evaluated (according to the semantics). Type
names can appear in expressions outside of 'sizeof' expressions,
in particular in cast expressions. They also appear in compound
literals (a compound literal may not have a variable length array
type, but it may have a variably modified type, causing evaluation
of the bound expression(s) in any contained VLA types).
I agree the Standard would benefit from a more explicit statement
about the semantics of evaluating a type name.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sizeof object got by dereferencing uniniitalized pointer "V.Subramanian, India" <subramanian100in@yahoo.com> - 2019-11-28 05:20 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-28 05:37 -0800
Re: sizeof object got by dereferencing uniniitalized pointer mark.bluemel@gmail.com - 2019-11-28 05:59 -0800
Re: sizeof object got by dereferencing uniniitalized pointer David Brown <david.brown@hesbynett.no> - 2019-11-28 15:51 +0100
Re: sizeof object got by dereferencing uniniitalized pointer Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-11-28 07:27 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Herbert Rosenau <nospam@hrosenau.de> - 2019-11-28 17:28 +0100
Re: sizeof object got by dereferencing uniniitalized pointer Thiago Adams <thiago.adams@gmail.com> - 2019-11-28 08:51 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-28 17:36 +0000
Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 16:37 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-29 01:27 +0000
Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 17:47 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-28 21:16 -0500
Re: sizeof object got by dereferencing uniniitalized pointer Keith Thompson <kst-u@mib.org> - 2019-11-29 00:38 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-29 09:03 -0500
Re: sizeof object got by dereferencing uniniitalized pointer Keith Thompson <kst-u@mib.org> - 2019-11-29 13:15 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Keith Thompson <kst-u@mib.org> - 2019-11-29 13:38 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-29 07:09 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-29 06:10 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-29 11:12 -0500
Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-12-01 09:32 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-29 07:39 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-29 22:00 +0000
Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-29 22:40 -0500
Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-30 11:19 +0000
Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-30 09:01 -0500
Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-12-01 10:28 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-28 12:49 -0500
Re: sizeof object got by dereferencing uniniitalized pointer Thiago Adams <thiago.adams@gmail.com> - 2019-11-28 11:26 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Richard Damon <Richard@Damon-Family.org> - 2019-11-28 17:19 -0500
Re: sizeof object got by dereferencing uniniitalized pointer Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-11-28 17:26 -0800
Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-28 22:34 +0000
Re: sizeof object got by dereferencing uniniitalized pointer Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-28 17:34 +0000
You gotta wonder why people say things like this! (Was: sizeof object got by dereferencing uniniitalized pointer) gazelle@shell.xmission.com (Kenny McCormack) - 2019-11-28 17:19 +0000
Re: sizeof object got by dereferencing uniniitalized pointer Keith Thompson <kst-u@mib.org> - 2019-11-28 14:55 -0800
Re: sizeof object got by dereferencing uniniitalized pointer "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-11-30 15:28 -0800
csiph-web