469,903 Members | 1,958 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

tryick use of sizeof again[Explaination Wanted]

char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'?
thanks

Sep 14 '07 #1
72 2677
On Sep 14, 11:16 am, goacr...@gmail.com wrote:
char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'
Who knows the vagarities of C++? Why not ask in comp.lang.c++ instead
of here? We don't deal with C++ here, only C
Sep 14 '07 #2
go******@gmail.com wrote:
char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'?
thanks
sizeof expressions are NOT evaluated. Only the type is important
to get the size of it.
Sep 14 '07 #3
"Lew Pitcher" <lp******@teksavvy.coma crit dans le message de news:
11**********************@w3g2000hsg.googlegroups.c om...
On Sep 14, 11:16 am, goacr...@gmail.com wrote:
>char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'

Who knows the vagarities of C++? Why not ask in comp.lang.c++ instead
of here? We don't deal with C++ here, only C
Why be unnecessarily obtuse when the issue is not C++ specific.

sizeof expr computes the size in bytes of the object denoted by expr without
producing code for evaluating that expression. Therefore ch is not
incremented.

--
Chqrlie.
Sep 14 '07 #4
jacob navia wrote:
go******@gmail.com wrote:
>char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'?
thanks

sizeof expressions are NOT evaluated. Only the type is important
to get the size of it.

indeed. Consider sizeof to be

size_t sizeof(type)
and not
size_t sizeof(int)

Sep 14 '07 #5
On Sep 14, 6:19 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
Why be unnecessarily obtuse when the issue is not C++ specific.
How do you know it is not C++ specific? For example, after

char c - '0';
c += sizeof ('0');

what is the value stored in c? There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

Sep 14 '07 #6
go******@gmail.com writes:
char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'?
If you're going to post code here, please make sure it's C, not C++.
It's also helpful to post a (small) complete program rather than an
isolated snippet.

'cout', '<<', and 'endl' are specific to C++.

Your underlying question happens, in this particular case, to be
equally valid in both languages, but there are subtle differences
between C and C++ that even the C experts here may not be aware of.

Just this once, I'll show you a C version of your program.

#include <stdio.h>
int main(void)
{
char ch = 'a';
int v = sizeof ++ch;
printf("%c\n", ch);
return 0;
}

Next time, please either post C in the first place, or post to
comp.lang.c++. If you're having trouble knowing the difference, you
need to learn one language or the other (perhaps eventually both).
A newsgroup is not a good place to do that, but
alt.comp.lang.learn.c-c++ might be more appropriate for your purposes
than comp.lang.c.

Several people have already given you the answer to your question --
which is unfortunate, since the question looks very much like
homework. An expression like 'sizeof ++ch' isn't something that you'd
write if your goal is to accomplish something. It was almost
certainly written by someone trying to make a specific point.

If you want us to do your homework for you, please post your
instructor's e-mail address so we can submit our solutions directly.
We're willing to provide hints if you've done some of the work
yourself and you're stuck on something, but we won't (usually) do it
for you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '07 #7
christian.bau wrote:
On Sep 14, 6:19 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
>Why be unnecessarily obtuse when the issue is not C++ specific.

How do you know it is not C++ specific? For example, after

char c - '0';
c += sizeof ('0');

what is the value stored in c? There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.
This is not the case for the problem at hand. Both C and C++
do not evaluate the expression of a sizeof.

Sep 14 '07 #8


sizeof is a handy way of ensuring that an expression is legal without
actually evaluating the expression. For instance, you could have code
something like the following:

typedef ... SomeType;

void Func(SomeType const *p)
{
sizeof (p->val); /* Ensure that SomeType has a member called
val */
}

Martin

Sep 14 '07 #9
There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.

Martin
Sep 14 '07 #10
jacob navia <ja***@jacob.remcomp.frwrites:
christian.bau wrote:
>On Sep 14, 6:19 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
>>Why be unnecessarily obtuse when the issue is not C++ specific.
How do you know it is not C++ specific? For example, after
char c - '0';
c += sizeof ('0');
what is the value stored in c? There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

This is not the case for the problem at hand. Both C and C++
do not evaluate the expression of a sizeof.
(I assume you mean the operand, not the expression.)

That's nearly correct. In C, if the type of the operand is a variable
length array type, then the operand is evaluated. C++, as far as I
know, doesn't have variable length arrays.

Of course, there are no VLAs in the posted code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '07 #11
Martin Wells wrote:

Please don't snip attributions.
christian.bau wrote:
>There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.


There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.
As Keith pointed out, VLAs muddy the waters.

#include <stdio.h>

int main(void) {
const int N = 42;

int data[N];

printf( "%d\n", sizeof data );
}

Gives the same result in C and C++, bit in the former the operand is
evaluated, in the latter it is not.

--
Ian Collins.
Sep 14 '07 #12
"christian.bau" wrote:
"Charlie Gordon" <n...@chqrlie.orgwrote:
>Why be unnecessarily obtuse when the issue is not C++ specific.

How do you know it is not C++ specific? For example, after

char c - '0';
c += sizeof ('0');

what is the value stored in c? There are subtle differences
between the sizeof operator in C and C++, and someone knowing
absolutely everything about C but nothing about C++ would most
likely not know the right answer to the problem I wrote.
Why did you snip away the expression that triggered the query?
I.e:
>> int v = sizeof ++ch;
which has the same answer in C and C++ (I think), i.e. that sizeof
does not implement (or evaluate) the expression, it simply
considers its type. The answer it gives will usually be different
in C and C++, because they are different languages and define
things differently. Which is also the reason for not asking C++
questions in c.l.c.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 14 '07 #13
"christian.bau" <ch***********@cbau.wanadoo.co.uka crit dans le message
de news: 11*********************@g4g2000hsf.googlegroups.co m...
On Sep 14, 6:19 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
>Why be unnecessarily obtuse when the issue is not C++ specific.

How do you know it is not C++ specific? For example, after

char c - '0';
c += sizeof ('0');

what is the value stored in c? There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.
Yeah right sizeof('a') == 1 in C++ and sizeof('a') == sizeof(int) in C (how
stupid and counter-intuitive!)

someone knowing absolutely everything about C but nothing about C++ would
fit my definition of obtuse perfectly.

--
Chqrlie.
Sep 14 '07 #14
"Martin Wells" <wa****@eircom.neta crit dans le message de news:
11**********************@k79g2000hse.googlegroups. com...
>There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.


There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.
Wrong!
But you snipped the code and attribution.

sizeof('1') evaluates differently in C and C++. In C it is equivalent to
sizeof(int), while in C++ it evaluates like sizeof(char). On most
architectures, this will yield a different value.

--
Chqrlie.
Sep 14 '07 #15
"Charlie Gordon" <ne**@chqrlie.orgwrites:
"Martin Wells" <wa****@eircom.neta crit dans le message de news:
11**********************@k79g2000hse.googlegroups. com...
>>There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.

Wrong!
But you snipped the code and attribution.

sizeof('1') evaluates differently in C and C++. In C it is equivalent to
sizeof(int), while in C++ it evaluates like sizeof(char). On most
architectures, this will yield a different value.
I would argue that that's not a difference in the behavior of the
sizeof operator. The operator behaves exactly the same in both
languages, yielding the size in bytes of its operand. The meaning of
the operand just happens to be different.

But it does illustrate the point that there are subtle differences
between the two languages. Asking about C++ code here in comp.lang.c
can sometimes lead to confusion; it will inevitably lead to arguments
like this one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '07 #16
"Keith Thompson" <ks***@mib.orga crit dans le message de news:
ln************@nuthaus.mib.org...
"Charlie Gordon" <ne**@chqrlie.orgwrites:
>"Martin Wells" <wa****@eircom.neta crit dans le message de news:
11**********************@k79g2000hse.googlegroups. com...
>>>There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.

Wrong!
But you snipped the code and attribution.

sizeof('1') evaluates differently in C and C++. In C it is equivalent to
sizeof(int), while in C++ it evaluates like sizeof(char). On most
architectures, this will yield a different value.

I would argue that that's not a difference in the behavior of the
sizeof operator. The operator behaves exactly the same in both
languages, yielding the size in bytes of its operand. The meaning of
the operand just happens to be different.
How can you demonstrate the true nature of 'h' without resorting to the
sizeof operator ?
The very reason it is an int is quite obscure, multibyte character constants
are not defined in any standard way, it would be foolish to use them.
But it does illustrate the point that there are subtle differences
between the two languages. Asking about C++ code here in comp.lang.c
can sometimes lead to confusion; it will inevitably lead to arguments
like this one.
I know. But isn't it more helpful to explain these subtle differences than
to brutally bash OPs for trespassing without cause ?

--
Chqrlie
Sep 15 '07 #17
"Charlie Gordon" <ne**@chqrlie.orgwrites:
"Keith Thompson" <ks***@mib.orga crit dans le message de news:
ln************@nuthaus.mib.org...
>"Charlie Gordon" <ne**@chqrlie.orgwrites:
>>"Martin Wells" <wa****@eircom.neta crit dans le message de news:
11**********************@k79g2000hse.googlegroups. com...
There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

There's no subtle differences between C and C++ in this respect. Or
even differences for that matter.

Wrong!
But you snipped the code and attribution.

sizeof('1') evaluates differently in C and C++. In C it is equivalent to
sizeof(int), while in C++ it evaluates like sizeof(char). On most
architectures, this will yield a different value.

I would argue that that's not a difference in the behavior of the
sizeof operator. The operator behaves exactly the same in both
languages, yielding the size in bytes of its operand. The meaning of
the operand just happens to be different.

How can you demonstrate the true nature of 'h' without resorting to the
sizeof operator ?
The very reason it is an int is quite obscure, multibyte character constants
are not defined in any standard way, it would be foolish to use them.
That's beside the point, which is that (as far as I know) sizeof
behaves identically in C and C++.
>But it does illustrate the point that there are subtle differences
between the two languages. Asking about C++ code here in comp.lang.c
can sometimes lead to confusion; it will inevitably lead to arguments
like this one.

I know. But isn't it more helpful to explain these subtle differences than
to brutally bash OPs for trespassing without cause ?
Not all of us *know* the subtle differences.

And I for one did not brutally bash the OP for posting C++ code.
I brutally bashed the OP for posting an apparent homework question.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '07 #18
[snippage]

In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.orgwrote:
>That's beside the point, which is that (as far as I know) sizeof
behaves identically in C and C++.
Well, one can argue this; but one can also argue otherwise, because
sizeof applied to a Variable Length Array in C99 is different. The
potential flaw in this second argument is that C++ does not have
VLAs (yet).

Even if the sizeof operator is considered "the same" (e.g., if one
is using C89 or C95), there are still subtle traps:

#include <stdio.h>

struct A { char block[8]; };

int main(void) {
struct B {
struct A {
char block[512];
} a;
};

printf("sizeof(struct A) = %lu\n",
(unsigned long) sizeof(struct A));
return 0;
}

Compile this program with a C compiler and with a C++ compiler,
and run the two executables:

% ln prog.c prog.c++
% cc -o cprog prog.c -O4 -W -Wall -ansi -pedantic
% g++ -o c++prog -O4 prog.c++ -W -Wall
% ./cprog
sizeof(struct A) = 512
% ./c++prog
sizeof(struct A) = 8
%

Exercise: explain the results. :-)
>Not all of us *know* the subtle differences.
Indeed, there are many I do not know, since the C++ language I
learned in the 1980s is not the C++ language we have today. I got
bitten by a version of the above difference once, though. It makes
an interesting example, I think.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Sep 15 '07 #19
On Fri, 14 Sep 2007 19:19:27 +0200, "Charlie Gordon"
<ne**@chqrlie.orgwrote in comp.lang.c:
"Lew Pitcher" <lp******@teksavvy.coma crit dans le message de news:
11**********************@w3g2000hsg.googlegroups.c om...
On Sep 14, 11:16 am, goacr...@gmail.com wrote:
char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'
Who knows the vagarities of C++? Why not ask in comp.lang.c++ instead
of here? We don't deal with C++ here, only C

Why be unnecessarily obtuse when the issue is not C++ specific.
Yes it is. Nowhere in the C standard does it define what "sizeof" is,
means, or does in C++. Now it may just happen that C++ defines sizeof
this way, but whether it does or not is 100% completely off-topic
here.
sizeof expr computes the size in bytes of the object denoted by expr without
producing code for evaluating that expression. Therefore ch is not
incremented.
Kindly quote chapter and verse from _any_ version of the C standard
that defines this, or any other behavior, for sizeof in C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 15 '07 #20
On Sep 15, 4:51 am, Keith Thompson <ks...@mib.orgwrote:
goacr...@gmail.com writes:
char ch='a';
int v=sizeof ++ch;
cout<<ch<<endl;// output: 'a'
why not 'b'?

If you're going to post code here, please make sure it's C, not C++.
It's also helpful to post a (small) complete program rather than an
isolated snippet.

'cout', '<<', and 'endl' are specific to C++.

Your underlying question happens, in this particular case, to be
equally valid in both languages, but there are subtle differences
between C and C++ that even the C experts here may not be aware of.

Just this once, I'll show you a C version of your program.

#include <stdio.h>
int main(void)
{
char ch = 'a';
int v = sizeof ++ch;
printf("%c\n", ch);
return 0;

}

Next time, please either post C in the first place, or post to
comp.lang.c++. If you're having trouble knowing the difference, you
need to learn one language or the other (perhaps eventually both).
A newsgroup is not a good place to do that, but
alt.comp.lang.learn.c-c++ might be more appropriate for your purposes
than comp.lang.c.

Several people have already given you the answer to your question --
which is unfortunate, since the question looks very much like
homework. An expression like 'sizeof ++ch' isn't something that you'd
write if your goal is to accomplish something. It was almost
certainly written by someone trying to make a specific point.

If you want us to do your homework for you, please post your
instructor's e-mail address so we can submit our solutions directly.
We're willing to provide hints if you've done some of the work
yourself and you're stuck on something, but we won't (usually) do it
for you.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Thanks for your detailed illustration.

However, unfortunately , this is NOT any homework.
Just some personal interest.

Suggest you be more tolerant to freshmen, even if you're expert. I'm
sorry to say you seemed to like a bear with a sore head from the above
words.

Of course, I'm sorry about the cout issue.

Sep 15 '07 #21
Ian:
int main(void) {
const int N = 42;

int data[N];

printf( "%d\n", sizeof data );

}

Gives the same result in C and C++, bit in the former the operand is
evaluated, in the latter it is not.
That's wrong -- the operand is evaluated in *neither* language.

In C++, this will print something like 168 (i.e. sizeof(int) * 42)
because N is a compile-time constant. In C89, the code won't compile.
In C99... well... I can't really say I care.

Martin

Sep 15 '07 #22
Martin Wells wrote:
Ian:
>int main(void) {
const int N = 42;

int data[N];

printf( "%d\n", sizeof data );

}

Gives the same result in C and C++, bit in the former the operand is
evaluated, in the latter it is not.

That's wrong -- the operand is evaluated in *neither* language.

In C++, this will print something like 168 (i.e. sizeof(int) * 42)
because N is a compile-time constant. In C89, the code won't compile.
In C99... well... I can't really say I care.
Like it or not, C99 is C.

--
Ian Collins.
Sep 15 '07 #23
"Jack Klein" <ja*******@spamcop.neta crit dans le message de news:
u0********************************@4ax.com...
On Fri, 14 Sep 2007 19:19:27 +0200, "Charlie Gordon"
<ne**@chqrlie.orgwrote in comp.lang.c:
>"Lew Pitcher" <lp******@teksavvy.coma crit dans le message de news:
11**********************@w3g2000hsg.googlegroups.c om...
On Sep 14, 11:16 am, goacr...@gmail.com wrote:
char ch='a';

int v=sizeof ++ch;

cout<<ch<<endl;// output: 'a'

why not 'b'

Who knows the vagarities of C++? Why not ask in comp.lang.c++ instead
of here? We don't deal with C++ here, only C

Why be unnecessarily obtuse when the issue is not C++ specific.

Yes it is. Nowhere in the C standard does it define what "sizeof" is,
means, or does in C++. Now it may just happen that C++ defines sizeof
this way, but whether it does or not is 100% completely off-topic
here.
>sizeof expr computes the size in bytes of the object denoted by expr
without
producing code for evaluating that expression. Therefore ch is not
incremented.

Kindly quote chapter and verse from _any_ version of the C standard
that defines this, or any other behavior, for sizeof in C++.
Well we can't say what it does for C++ from the C standard, big deal!

At least we can explain what it does in C for the two lines

char ch = 'a';
int v = sizeof ++ch;

The expression ++ch is _not_ evaluated, only its type is determined by the
compiler to be char, which by definition has a size of 1. ch is unchanged.

Here are the appropriate quotations for the C language.

6.5.3.4 The sizeof operator

p2 semantics

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.

Annex J (informative)

J.1 Unspecified behavior

The following are unspecified:

Whether or not a size expression is evaluated when it is part of the operand
of a
sizeof operator and changing the value of the size expression would not
affect the
result of the operator (6.7.5.2).

For C++, ask the appropriate group, I suspect the interpretation will be the
same, but I am not an expert at C++.

--
Chqrlie.
Sep 15 '07 #24
Martin Wells wrote:
Ian:
>int main(void) {
const int N = 42;

int data[N];

printf( "%d\n", sizeof data );

}

Gives the same result in C and C++, bit in the former the operand is
evaluated, in the latter it is not.

That's wrong -- the operand is evaluated in *neither* language.

In C++, this will print something like 168 (i.e. sizeof(int) * 42)
because N is a compile-time constant. In C89, the code won't compile.
In C99... well... I can't really say I care.
However the following minor correction functions under C90:

#include <stdio.h>

int main(void) {
# define N 42
int data[N];

printf("%lu\n", (unsigned long)sizeof data);
return 0;
}
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 15 '07 #25
Chris Torek wrote:
>
.... snip ...
>
Even if the sizeof operator is considered "the same" (e.g., if one
is using C89 or C95), there are still subtle traps:

#include <stdio.h>

struct A { char block[8]; };

int main(void) {
struct B {
struct A {
char block[512];
} a;
};

printf("sizeof(struct A) = %lu\n",
(unsigned long) sizeof(struct A));
return 0;
}

Compile this program with a C compiler and with a C++ compiler,
and run the two executables:

% ln prog.c prog.c++
% cc -o cprog prog.c -O4 -W -Wall -ansi -pedantic
% g++ -o c++prog -O4 prog.c++ -W -Wall
% ./cprog
sizeof(struct A) = 512
% ./c++prog
sizeof(struct A) = 8
%

Exercise: explain the results. :-)
Obviously the local scope struct A is in effect for the printf
call, but as far as I am concerned this is wrong. That scope
should only appear when a struct B is open. I.e. the 512 result
should only appear for "sizeof struct B.a". In fact, since there
is no way to create an object of that type, it should not be
'sizable' at all.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 15 '07 #26
Charlie Gordon wrote:
>
.... snip ...
>
How can you demonstrate the true nature of 'h' without resorting
to the sizeof operator ? The very reason it is an int is quite
obscure, multibyte character constants are not defined in any
standard way, it would be foolish to use them.
What's obscure? chars are represented by an integer value. The
obvious place to hold an integer value is an int, provided it has
the capacity. Any other choice would cause program slowdowns in
one way or another. I never heard of any computer that held things
other than integers, although those integers (or collections of
integers) can be interpreted in various esoteric ways.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 15 '07 #27
Martin Wells <wa****@eircom.netwrites:
>int main(void) {
const int N = 42;

int data[N];

printf( "%d\n", sizeof data );

}

Gives the same result in C and C++, bit in the former the operand is
evaluated, in the latter it is not.

That's wrong -- the operand is evaluated in *neither* language.

In C++, this will print something like 168 (i.e. sizeof(int) * 42)
because N is a compile-time constant. In C89, the code won't compile.
In C99... well... I can't really say I care.
Since I do care ...

In C99, N is not a constant expression (though it's very likely to be
evaluted at compile time anyway), and 'data' is a VLA, therefore the
argument to sizeof *is* evaluated.

But 'data' is just an object, and the result of evaluating it is
discarded, since sizeof still depends only on the type of its
argument. So what difference does it make whether the argument is
evaluated or not?

The only case I can think of is where the VLA is volatile.

Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '07 #28
go******@gmail.com writes:
On Sep 15, 4:51 am, Keith Thompson <ks...@mib.orgwrote:
[...]
>Several people have already given you the answer to your question --
which is unfortunate, since the question looks very much like
homework. An expression like 'sizeof ++ch' isn't something that you'd
write if your goal is to accomplish something. It was almost
certainly written by someone trying to make a specific point.

If you want us to do your homework for you, please post your
instructor's e-mail address so we can submit our solutions directly.
We're willing to provide hints if you've done some of the work
yourself and you're stuck on something, but we won't (usually) do it
for you.

Thanks for your detailed illustration.

However, unfortunately , this is NOT any homework.
Just some personal interest.
Ok. But we do get a lot of people posting obvious homework questions
here, showing no sign of any effort to try to do the work themselves.
People even copy-and-paste their actual homework questions, without
indicating that they're homework.

And, as I said, your question *looks like* homework. I'm glad to
learn that it isn't.

Incidentally, when you post a followup, please delete any quoted
material that isn't relevant to your followup, particularly thee
signature (the stuff at the end following the "-- " delimiter).
Thanks.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '07 #29
CBFalconer wrote:
Martin Wells wrote:
>Ian:
>>int main(void) {
const int N = 42;

int data[N];

printf( "%d\n", sizeof data );

}

Gives the same result in C and C++, bit in the former the operand is
evaluated, in the latter it is not.
That's wrong -- the operand is evaluated in *neither* language.

In C++, this will print something like 168 (i.e. sizeof(int) * 42)
because N is a compile-time constant. In C89, the code won't compile.
In C99... well... I can't really say I care.

However the following minor correction functions under C90:
It also defeats the purpose of the example.
#include <stdio.h>

int main(void) {
# define N 42
int data[N];

printf("%lu\n", (unsigned long)sizeof data);
return 0;
}


--
Ian Collins.
Sep 15 '07 #30
CBFalconer wrote:
Chris Torek wrote:
.... snip ...
>Even if the sizeof operator is considered "the same" (e.g., if one
is using C89 or C95), there are still subtle traps:

#include <stdio.h>

struct A { char block[8]; };

int main(void) {
struct B {
struct A {
char block[512];
} a;
};

printf("sizeof(struct A) = %lu\n",
(unsigned long) sizeof(struct A));
return 0;
}
Exercise: explain the results. :-)

Obviously the local scope struct A is in effect for the printf
call, but as far as I am concerned this is wrong. That scope
should only appear when a struct B is open. I.e. the 512 result
should only appear for "sizeof struct B.a". In fact, since there
is no way to create an object of that type, it should not be
'sizable' at all.
I guess the lack of syntax to express the scope is a hole in C. In C++
one would write sizeof(struct B::A), or simply sizeof(B::A).

--
Ian Collins.
Sep 15 '07 #31
On 2007-09-14, Martin Wells <wa****@eircom.netwrote:
>

sizeof is a handy way of ensuring that an expression is legal without
actually evaluating the expression. For instance, you could have code
something like the following:

typedef ... SomeType;

void Func(SomeType const *p)
{
sizeof (p->val); /* Ensure that SomeType has a member called
val */
}
When would someone want to do something like that? I mean, why wouldn't you know
if SomeType has a val member or not? You can determine it before compilation.
Sep 15 '07 #32
On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
Martin Wells <wa****@eircom.netwrites:
>>int main(void) {
const int N = 42;

int data[N];

printf( "%d\n", sizeof data );

}

Gives the same result in C and C++, bit in the former the operand is
evaluated, in the latter it is not.

That's wrong -- the operand is evaluated in *neither* language.

In C++, this will print something like 168 (i.e. sizeof(int) * 42)
because N is a compile-time constant. In C89, the code won't compile.
In C99... well... I can't really say I care.

Since I do care ...

In C99, N is not a constant expression (though it's very likely to be
evaluted at compile time anyway), and 'data' is a VLA, therefore the
argument to sizeof *is* evaluated.

But 'data' is just an object, and the result of evaluating it is
discarded, since sizeof still depends only on the type of its argument.
So what difference does it make whether the argument is evaluated or
not?

The only case I can think of is where the VLA is volatile.

Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?
#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;

char nonvla[constant_size];
char vla[nonconstant_size];

printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);

return 0;
}

This should print:

sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1
Sep 15 '07 #33
Harald van Dijk wrote:
On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
>>
Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?

#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;

char nonvla[constant_size];
char vla[nonconstant_size];

printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);

return 0;
}

This should print:

sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1
Should it?

--
Ian Collins.
Sep 15 '07 #34
Keith Thompson wrote, On 15/09/07 20:52:

<snip>
Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?
size_t size=5
int array[++size];
--
Flash Gordon
Sep 15 '07 #35
On Sun, 16 Sep 2007 08:48:13 +1200, Ian Collins wrote:
Harald van Dijk wrote:
>On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
>>>
Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?

#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;

char nonvla[constant_size];
char vla[nonconstant_size];

printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);

return 0;
}

This should print:

sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1

Should it?
This is the behaviour mandated by the standard and implemented by the
compiler I'm using. If you believe the standard would have been better if
sizeof on VLAs were specified differently, then I don't necessarily
disagree, but if you believe the standard requires (or allows) any
different output, then could you please explain why?
Sep 15 '07 #36
On Sat, 15 Sep 2007 21:05:53 +0100, Flash Gordon wrote:
Keith Thompson wrote, On 15/09/07 20:52:

<snip>
>Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?

size_t size=5
int array[++size];
No, sizeof(array) will always return 6 in this case. The array size is
evaluated when the declaration is reached, but not re-evaluated
afterwards.
Sep 15 '07 #37
"Harald van D)&k" <tr*****@gmail.comwrites:
On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
[...]
>In C99, N is not a constant expression (though it's very likely to be
evaluted at compile time anyway), and 'data' is a VLA, therefore the
argument to sizeof *is* evaluated.

But 'data' is just an object, and the result of evaluating it is
discarded, since sizeof still depends only on the type of its argument.
So what difference does it make whether the argument is evaluated or
not?

The only case I can think of is where the VLA is volatile.

Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?

#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;

char nonvla[constant_size];
char vla[nonconstant_size];

printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);

return 0;
}

This should print:

sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1
Nope.

Here's what the standard says (C99 6.5.3.4p2):

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.

The type of the operand in both of your sizeof expressions is char,
not a VLA type. f() is never called. (I tried it, and the behavior
under the implementation I used is consistent with this.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '07 #38
On Sat, 15 Sep 2007 14:35:46 -0700, Keith Thompson wrote:
"Harald van D)&k" <tr*****@gmail.comwrites:
>On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
[...]
>>Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?

#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;

char nonvla[constant_size];
char vla[nonconstant_size];

printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);

return 0;
}
The type of the operand in both of your sizeof expressions is char, not
a VLA type.
I think you've overlooked the &. The operands to both sizeof operators
have array types.
Sep 15 '07 #39
On Sep 14, 10:00 pm, jacob navia <ja...@jacob.remcomp.frwrote:
christian.bau wrote:
On Sep 14, 6:19 pm, "Charlie Gordon" <n...@chqrlie.orgwrote:
Why be unnecessarily obtuse when the issue is not C++ specific.
How do you know it is not C++ specific? For example, after
char c - '0';
c += sizeof ('0');
what is the value stored in c? There are subtle differences between
the sizeof operator in C and C++, and someone knowing absolutely
everything about C but nothing about C++ would most likely not know
the right answer to the problem I wrote.

This is not the case for the problem at hand. Both C and C++
do not evaluate the expression of a sizeof.
How do you know that C++ doesn't evaluate the expression? Show me
exactly the page and line in the C Standard that tells us that C++
doesn't evaluate the argument of the sizeof operator.

Oh, you can't do that. There is no such statement in the C Standard.
There may be one in the C++ Standard, and I am sure the guys who read
comp.lang.c++ know everything about it, but this is comp.lang.c.
Sep 15 '07 #40
Harald van Dijk wrote:
On Sun, 16 Sep 2007 08:48:13 +1200, Ian Collins wrote:
>Harald van Dijk wrote:
>>On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?
#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;

char nonvla[constant_size];
char vla[nonconstant_size];

printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);

return 0;
}

This should print:

sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1
Should it?

This is the behaviour mandated by the standard and implemented by the
compiler I'm using. If you believe the standard would have been better if
sizeof on VLAs were specified differently, then I don't necessarily
disagree, but if you believe the standard requires (or allows) any
different output, then could you please explain why?
My thinking was the same as Keith's.

I just tried it with a couple of compilers and got

sizeof (&nonvla)[f()] = 1
sizeof (&vla)[f()] = 1

from both.

--
Ian Collins.
Sep 15 '07 #41
On Sun, 16 Sep 2007 10:15:31 +1200, Ian Collins wrote:
Harald van Dijk wrote:
>On Sun, 16 Sep 2007 08:48:13 +1200, Ian Collins wrote:
>>Harald van Dijk wrote:
On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?
#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;

char nonvla[constant_size];
char vla[nonconstant_size];

printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);

return 0;
}

This should print:

sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1
Should it?

This is the behaviour mandated by the standard and implemented by the
compiler I'm using. If you believe the standard would have been better
if sizeof on VLAs were specified differently, then I don't necessarily
disagree, but if you believe the standard requires (or allows) any
different output, then could you please explain why?

My thinking was the same as Keith's.
That the operand to sizeof is a char? If you change the array length from
1 to 2 you will see the printed size change; if it had type char, it
would remain 1.
I just tried it with a couple of compilers and got

sizeof (&nonvla)[f()] = 1
sizeof (&vla)[f()] = 1

from both.
Which compilers are those? GCC 4.1 doesn't call f. GCC 4.2 and Intel CC
10.0 do.
Sep 15 '07 #42
Harald van Dijk wrote, On 15/09/07 22:13:
On Sat, 15 Sep 2007 21:05:53 +0100, Flash Gordon wrote:
>Keith Thompson wrote, On 15/09/07 20:52:

<snip>
>>Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?
size_t size=5
int array[++size];

No, sizeof(array) will always return 6 in this case. The array size is
evaluated when the declaration is reached, but not re-evaluated
afterwards.
I meant to say
sizeof array[++size];
Obviously I my brain was disconnected at the time.
--
Flash Gordon
Checking down the back of the sofa to see if my brain is there.
Sep 15 '07 #43
Harald van Dijk wrote:
>
>I just tried it with a couple of compilers and got

sizeof (&nonvla)[f()] = 1
sizeof (&vla)[f()] = 1

from both.

Which compilers are those? GCC 4.1 doesn't call f. GCC 4.2 and Intel CC
10.0 do.
gcc 4.0.2 and Sun cc.

--
Ian Collins.
Sep 15 '07 #44
On Sat, 15 Sep 2007 23:13:33 +0100, Flash Gordon wrote:
Harald van Dijk wrote, On 15/09/07 22:13:
>On Sat, 15 Sep 2007 21:05:53 +0100, Flash Gordon wrote:
>>Keith Thompson wrote, On 15/09/07 20:52:

<snip>

Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?
size_t size=5
int array[++size];

No, sizeof(array) will always return 6 in this case. The array size is
evaluated when the declaration is reached, but not re-evaluated
afterwards.

I meant to say
sizeof array[++size];
Obviously I my brain was disconnected at the time.
array[++size] is an expression of type int, so ++size will not be
evaluated.
Sep 15 '07 #45
Harald van D k <true...@gmail.comwrote:
Ian Collins wrote:
Harald van D k wrote:
Keith Thompson wrote:
Can anyone think of a case where the evaluation of the
argument of sizeof (when it's a VLA) has a visible
effect on the behavior of the program?
>
#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;
>
char nonvla[constant_size];
char vla[nonconstant_size];
>
printf("sizeof (&nonvla)[f()] = %zu\n",
sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n",
sizeof (&vla)[f()]);
>
return 0;
}
>
This should print:
>
sizeof (&nonvla)[f()] = 1
f is called!
sizeof (&vla)[f()] = 1
Should it?

This is the behaviour mandated by the standard and
implemented by the compiler I'm using. If you believe
the standard would have been better if sizeof on VLAs
were specified differently, then I don't necessarily
disagree, but if you believe the standard requires (or
allows) any different output, then could you please
explain why?
6.5.3.4p2: "... 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."

Since you don't apply sizeof to an operand of variable
length array type, I don't see how or why f should be
called.

I believe the evaluation clause in 6.5.3.4p2 is more for
cases like...

sizeof (int [nonconstexpr])

Since pointer arithmetic is required to work on VLAs,
such pointers (including those created by using the name
of a VLA) necessarily carry information about the VLAs
size, either directly or indirectly.

Consider...

#include <stdio.h>
#include <stdlib.h>

int foo()
{
return 1 + rand() % 10;
}

size_t bah(char (*vlap)[*])
{
return sizeof *vlap;
}

int main()
{
int vla[foo()];
printf("%zu\n", bah(&vla));
return 0;
}

The function foo() is only called once. A compiler that
calls foo() twice would be broken.

--
Peter

Sep 16 '07 #46
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?
Gcc with flags "-std=c99 -pedantic -Wall' thinks the following program
is legal:

#include <stdio.h>

int main(void)
{
int n = 42;
printf("%d\n", (int)sizeof(char [n++]));
printf("%d\n", (int)sizeof(char [n++]));
return 0;
}

and it prints:

42
43

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 16 '07 #47
Harald van Dk <tr*****@gmail.comwrites:
On Sat, 15 Sep 2007 14:35:46 -0700, Keith Thompson wrote:
>"Harald van D)&k" <tr*****@gmail.comwrites:
>>On Sat, 15 Sep 2007 12:52:39 -0700, Keith Thompson wrote:
[...]
>>>Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?

#include <stdio.h>
int f(void) {
puts("f is called!");
return 0;
}
int main(void) {
enum { constant_size = 1 };
const int nonconstant_size = 1;

char nonvla[constant_size];
char vla[nonconstant_size];

printf("sizeof (&nonvla)[f()] = %zu\n", sizeof (&nonvla)[f()]);
printf("sizeof (&vla)[f()] = %zu\n", sizeof (&vla)[f()]);

return 0;
}
The type of the operand in both of your sizeof expressions is char, not
a VLA type.

I think you've overlooked the &. The operands to both sizeof operators
have array types.
You're right (and gcc gets this wrong).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 16 '07 #48
Harald van Dijk wrote, On 16/09/07 00:52:
On Sat, 15 Sep 2007 23:13:33 +0100, Flash Gordon wrote:
>Harald van Dijk wrote, On 15/09/07 22:13:
>>On Sat, 15 Sep 2007 21:05:53 +0100, Flash Gordon wrote:
Keith Thompson wrote, On 15/09/07 20:52:

<snip>

Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?
size_t size=5
int array[++size];
No, sizeof(array) will always return 6 in this case. The array size is
evaluated when the declaration is reached, but not re-evaluated
afterwards.
I meant to say
sizeof array[++size];
Obviously I my brain was disconnected at the time.

array[++size] is an expression of type int, so ++size will not be
evaluated.
I've definitely been working too many hours this week. Normal service
will be restored once I've taken my brain out and washed all the rubbish
out of it.
--
Flash Gordon
Sep 16 '07 #49
"Richard Tobin" <ri*****@cogsci.ed.ac.uka crit dans le message de news:
fc***********@pc-news.cogsci.ed.ac.uk...
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>Can anyone think of a case where the evaluation of the argument of
sizeof (when it's a VLA) has a visible effect on the behavior of the
program?

Gcc with flags "-std=c99 -pedantic -Wall' thinks the following program
is legal:

#include <stdio.h>

int main(void)
{
int n = 42;
printf("%d\n", (int)sizeof(char [n++]));
printf("%d\n", (int)sizeof(char [n++]));
return 0;
}

and it prints:

42
43
While it seems logic to do so, the language of the standard is not clear:

6.5.3.4p2: "... 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."

What if the type of char[n++] ?
How can we evaluate char[n++] ?

char [n++] *is* a type, it does not *have* a type.

I don't think the language of the standard correctly specifies what should
be the behaviour of such code.

A more compelling example would be

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
if (argc 1) {
int n = strtol(argv[1], NULL, 0);
int matrix[n][n];
int i = 1;

printf("i=%d, n=%d\n", i, n);
printf ("sizeof(matrix) = %zd\n", sizeof(matrix));
printf ("sizeof(matrix[i++]) = %zd\n", sizeof(matrix[i++]));
printf("i=%d\n", i);

printf ("sizeof(char[i++]) = %zd\n", sizeof(char[i++]));
printf("i=%d\n", i);
}
return 0;
}

with gcc upto version 4.1.3, I get what I interpret as non conforming
behaviour:

$ ./vla 2
i=1, n=2
sizeof(matrix) = 16
sizeof(matrix[i++]) = 8
i=1
sizeof(char[i++]) = 1
i=2

I think we have a defect here: the argument to sizeof should be evaluated is
it *is* a variable length array type, not if it is an expression whois type
can be determined from static analysis.

--
Chqrlie.
Sep 16 '07 #50

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.