Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Regarding sizeof Operator

Reply
Thread Tools

Regarding sizeof Operator

 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-14-2006
Richard G. Riley said:

> On 2006-03-14, Robert Gamble <> wrote:
>>
>> Did you even read the section you posted?

>
> <sarcasm on> No. I posted it totally blindly just guessing it had
> something to do with the OP. I guess I was luck I hit on something to
> do with sizeof eh? <sarcasm off>


I see very little sarcasm there.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
 
Jordan Abel
Guest
Posts: n/a
 
      03-14-2006
On 2006-03-14, Richard Heathfield <> wrote:
> Richard G. Riley said:
>
>> "sizeof" operator does not evaluate the expression contained therein
>> it seems.

>
> Correct.
>
>> I didnt know that :

>
> Evidently.
>
>> but purely because I never stuck an
>> expression in it for reasons unknown - I just assumed it as a
>> "preprocessor" type thing and just never did it.

>
> It is nothing to do with the preprocessor.
>
>
>> I can claim no genius.

>
> Evidently.
>
>>
>> It would be the same had you put
>>
>> sizeof(x=3);

>
> Correct.
>
>>
>> I would agree that it is a little confusing

>
> I wouldn't.
>
>> since the
>> compiler (gcc) happily lets you write something like
>>
>> sizeof(x=funcCall(y)) too!

>
> It's supposed to.
>
>>
>> Looking at this in n1124pdf:
>>
>> 6.5.3.4 The sizeof operator
>> Constraints
>>
>> 2 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 ....


Out of curiosity, is

sizeof cos() [no prototype in scope] allowed [sizeof(int)], or is it still
undefined behavior?

That is, is it the evaluation of a function that the compiler has
"wrong" what causes undefined behavior, or is it the presence in the
source of the call itself?

how about

sizeof(((int(*)())0)())?
 
Reply With Quote
 
 
 
 
Micah Cowan
Guest
Posts: n/a
 
      03-14-2006
Jordan Abel <> writes:

> On 2006-03-14, Richard Heathfield <> wrote:
> Out of curiosity, is
>
> sizeof cos() [no prototype in scope] allowed [sizeof(int)], or is it still
> undefined behavior?


Well, implicit declarations were removed in C99, so it's a constraint
violation.

I was going to say "it's fine" for C90, but I gave it a little more
thought after your next sentence. Because, whether or not it is
actually evaluated, it results in the implicit declaration.

> That is, is it the evaluation of a function that the compiler has
> "wrong" what causes undefined behavior, or is it the presence in the
> source of the call itself?


It is the declaration of cos() that causes undefined behavior. cos is
a reserved identifier (7.1.3#1), and is only allowed to refer to the
standard library function. Since your cos does not, it violates
this rule. Rules 6.2.2#2 and 6.2.7#2 apply when it's a function you've
defined yourself. These are C99 references, but they appear
equivalently in C90.

However, in C90, sizeof(foo()) would be allowed (if you never define
foo()). The implicit declaration is fine, because C90 3.7
(corresponding to C99 6.9) only requires a definition to exist if you
use its identifier in an expression other than the operand of a sizeof.

> how about
>
> sizeof(((int(*)())0)())?


Perfectly fine. The expression is never evaluated.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The sizeof operator : sizeof(++i) Kislay C Programming 10 10-19-2007 09:28 PM
regarding sizeof operator for class with no data variables pratap C++ 3 08-20-2007 08:18 AM
regarding sizeof operator for class with no data variables pratap C++ 1 08-20-2007 04:23 AM
regarding sizeof operator for class with no data variables pratap C++ 0 08-19-2007 06:25 PM
Regarding sizeof operator venkat C Programming 3 03-05-2007 01:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57