473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

About sizeof


int i = 0;
i = sizeof( i++ );

What's the result of i¡H

I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is

undefined behavior.

I know that i = i++ is undefined behavior,

but sizeof is calculated at compiling time.

--
|
 ___
(-_-)
<¡ä¡ä>¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w¢w ªÅ´ßªª³õ shepjeng.twbbs.org ¢w¢w¢w
¡þ plum.cs.nccu.edu.tw
Nov 13 '05 #1
17 4212
Someone whose name my newsreader would probably mangle wrote:
int i = 0;
i = sizeof( i++ ); I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is
undefined behavior. I know that i = i++ is undefined behavior,
but sizeof is calculated at compiling time.


But you're still changing the value of i twice in one statement[1].
i++ increments it. i = sizeof... sets it to a constant value.
Undefined behavior. Anything can happen. The computer can whistle
"Night in Tunisia", make a pot of coffee, and explode.

This ignores the question of whether sizeof (i++) is legal or
well-defined. I don't know that one.

[1] To be precise, without an intervening sequence point.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #2
Tom Zych wrote:
Someone whose name my newsreader would probably mangle wrote:
int i = 0;
i = sizeof( i++ );
I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is
undefined behavior.

I know that i = i++ is undefined behavior,
but sizeof is calculated at compiling time.


But you're still changing the value of i twice in one statement[1].
i++ increments it. i = sizeof... sets it to a constant value.
Undefined behavior. Anything can happen. The computer can whistle
"Night in Tunisia", make a pot of coffee, and explode.


No, sizeof does not evaluate i++.

6.5.3.4 The sizeof operator
Semantics
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 ofthe 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.
This ignores the question of whether sizeof (i++) is legal or
well-defined. I don't know that one.


The type of i++ is int, therefore it's equivalent to sizeof(int).

Jirka

Nov 13 '05 #3
On Thu, 11 Sep 2003, Tom Zych wrote:
Someone whose name my newsreader would probably mangle wrote:
int i = 0;
i = sizeof( i++ );

I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is
undefined behavior.

I know that i = i++ is undefined behavior,
but sizeof is calculated at compiling time.


But you're still changing the value of i twice in one statement[1].
i++ increments it. i = sizeof... sets it to a constant value.


No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
to sizeof( i ).

--
au***@axis.com
Nov 13 '05 #4
In <48********@shepjeng.twbbs.org> GA************@shepjeng.twbbs.org (³á³á³á³á) writes:

int i = 0;
i = sizeof( i++ );

What's the result of i¡H

I think the result may be sizeof( int ) or sizeof( int ) + 1, or it is
undefined behavior.
It's sizeof(int). When the argument of sizeof is an expression (other
than a C99 VLA), it is NOT evaluated, the compiler merely determines its
type and produces the appropriate result.
I know that i = i++ is undefined behavior,
This is a completely different issue.
but sizeof is calculated at compiling time.


It need not be, unless used in a context where a constant expression is
required.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
Johan Aurér wrote:
....
No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
to sizeof( i ).


That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );

Jirka

Nov 13 '05 #6

"³á³á³á³á" <GA************@shepjeng.twbbs.org> wrote in message
news:48********@shepjeng.twbbs.org...

int i = 0;
i = sizeof( i++ );

What's the result of i¡H


Using sizeof to "i++ " is similar to "sizeof(int)". i++ returns an integer,
so your compiler just think about "what is the size of integer in this
implementation"

try this

printf("The size is %d \n", sizeof(i*32/9 + 2525 - i++ * i--));
--
Jeff
-je6543 at yahoo.com
Nov 13 '05 #7
Jirka Klaue wrote:
No, sizeof does not evaluate i++.


Oh. Ok, my bad.

Have to remember that sizeof(i++) for the IOCCC. It's no good for
anything else :)

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #8
# int i = 0;
# i = sizeof( i++ );
#
# What's the result of i¡H

'Doctor! Doctor! It hurts when I do this!'
'Then don't that.'

Is this like a review of obfustucated C contest code, or exams of a sadistic
instructor, or malevolent job interviewers? I see people bring up truly twisted
code that no programmer would ever use in production code, and then ask
'what does this do?' I'm left wonderring, why do this people even want to know?

--
Derk Gwen http://derkgwen.250free.com/html/index.html
But I do believe in this.
Nov 13 '05 #9
On Thu, 11 Sep 2003 19:14:34 +0200, Jirka Klaue
<jk****@ee.tu-berlin.de> wrote in comp.lang.c:
Johan Aurér wrote:
...
No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
to sizeof( i ).


That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );

Jirka


Works just fine on the TI DSP compiler I'm working with at the moment.

If fact this one will not assert either, on this particular
implementation:

char i;
assert (sizeof(i) == sizeof(i + 1));

CHAR_BIT is 16 and char, short, and int all have identical
representations.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #10
Jirka Klaue <jk****@ee.tu-berlin.de> wrote in message news:<3F**************@ee.tu-berlin.de>...
Johan Aurér wrote:
...
No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
to sizeof( i ).


That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );

Jirka


Why does sizeof i + 1 give 3 and not the size of an int on my system?
I thought it does not evaluate its operand? This makes no sense.
Nov 13 '05 #11
Mantorok Redgormor wrote:
Why does sizeof i + 1 give 3 and not the size of an int on my system?
I thought it does not evaluate its operand? This makes no sense.


Because sizeof has a higher precedence than +. Your expression is
equivalent to (sizeof i) + 1.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #12
ne*****@tokyo.com (Mantorok Redgormor) wrote:
Jirka Klaue <jk****@ee.tu-berlin.de> wrote in message news:<3F**************@ee.tu-berlin.de>...
Johan Aurér wrote:
...
> No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
> to sizeof( i ).


That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );

Jirka


Why does sizeof i + 1 give 3 and not the size of an int on my system?
I thought it does not evaluate its operand? This makes no sense.


Hehehe, you calculated: ( sizeof i ) + 1 , which gives a result
quite different from: sizeof ( i + 1 ).

Caught by operator precedence... :-)

Irrwahn

--
I can't see it from here, but it looks good to me.
Nov 13 '05 #13

"Derk Gwen" <de******@HotPOP.com> wrote in message
news:vm************@corp.supernews.com...
# int i = 0;
# i = sizeof( i++ );
#
# What's the result of i¡H

'Doctor! Doctor! It hurts when I do this!'
'Then don't that.'

Is this like a review of obfustucated C contest code, or exams of a sadistic instructor, or malevolent job interviewers? I see people bring up truly twisted code that no programmer would ever use in production code, and then ask
'what does this do?' I'm left wonderring, why do this people even want to know?


When one day someone write such kind of code, and you have to understand it
or explain it to your children :-)

--
Jeff
-je6543 at yahoo.com
Nov 13 '05 #14
Jack Klein wrote:
Jirka Klaue wrote:
Johan Aurér wrote:
No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
to sizeof( i ).


That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );


Works just fine on the TI DSP compiler I'm working with at the moment.

If fact this one will not assert either, on this particular
implementation:

char i;
assert (sizeof(i) == sizeof(i + 1));

CHAR_BIT is 16 and char, short, and int all have identical
representations.


That's nice. This compiler isn't a free-standing implementation, is it?
Otherwise EOF is going to be a serious problem.
And I wouldn't call it "general".

Jirka

Nov 13 '05 #15
Jirka Klaue wrote:
Jack Klein wrote: ....
char i;
assert (sizeof(i) == sizeof(i + 1));

CHAR_BIT is 16 and char, short, and int all have identical
representations.


That's nice. This compiler isn't a free-standing implementation, is it?


s/free-standing/hosted/
Otherwise EOF is going to be a serious problem.
And I wouldn't call it "general".


Jirka

Nov 13 '05 #16
In <bj**********@mamenchi.zrz.TU-Berlin.DE> Jirka Klaue <jk****@ee.tu-berlin.de> writes:
Jack Klein wrote:

Works just fine on the TI DSP compiler I'm working with at the moment.

If fact this one will not assert either, on this particular
implementation:

char i;
assert (sizeof(i) == sizeof(i + 1));

CHAR_BIT is 16 and char, short, and int all have identical
representations.
That's nice. This compiler isn't a free-standing implementation, is it?


Of course it is. I doubt anyone would attempt a hosted implementation
with UCHAR_MAX > INT_MAX and/or a general purpose computer with a DSP
as its main CPU.
Otherwise EOF is going to be a serious problem.


Because the standard C library specification is written with the implicit
assumption that UCHAR_MAX <= INT_MAX. I've never understood why they
didn't make it explicit.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #17
Mantorok Redgormor <ne*****@tokyo.com> wrote:
Jirka Klaue <jk****@ee.tu-berlin.de> wrote in message news:<3F**************@ee.tu-berlin.de>...
Johan Aurer wrote:
...
> No, sizeof does not evaluate its operand, so sizeof( i++ ) is equivalent
> to sizeof( i ).
That's true in this case, but not generally:

short i;
assert( sizeof(i) == sizeof (i + 1) );

Jirka

Why does sizeof i + 1 give 3 and not the size of an int on my system?
I thought it does not evaluate its operand? This makes no sense.


This makes absolute sense.

sizeof i + 1 == (sizeof i) + 1

....which is quite different from:

sizeof (i + 1)

Alex
Nov 13 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sims | last post by:
Hi, I have some small questions that have never been any problems, (for my compiler?), but always make me curious. So here goes... what does the standard sday about the 'if' statement? for...
2
by: Xiangliang Meng | last post by:
Hi, all. What will we get from sizeof(a class without data members and virtual functions)? For example: class abnormity { public: string name() { return "abnormity"; }
36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
23
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
5
by: Donkey | last post by:
Hello, I want to find out how many digits does each date type have and how many bytes does it occupy in memory, so I wrote a program below: #include <stdio.h> const long double...
15
by: sethukr | last post by:
Hi everybody, While running the following program in GCC, i'm very much screwed. main() { char *ptr1; char arr; int i; char *ptr2;
43
by: bharath539 | last post by:
how much memory is allocated for following structure struct bharath { int b; char c; float d; } and how?
6
by: sabb_tange_jange | last post by:
plz sir solve a problem for me void main() { char ch='f'; printf("%d",sizeof('f')); } it will give us output 2 bytes not 1 byte that is of character variable...
15
by: SM | last post by:
Hello, I have another simple question about an array in PHP and a variable in PHP. This is the array: $thumbs_cat_1 = array( 'wine', 'cheese', 'ice',
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.