tryick use of sizeof again[Explaination Wanted]

L

Lew Pitcher

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
 
J

jacob navia

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.
 
C

Charlie Gordon

Lew Pitcher said:
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.
 
P

Punkie

jacob said:
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)
 
C

christian.bau

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.
 
C

CBFalconer

christian.bau said:
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:

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.
 
K

Keith Thompson

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.
 
J

jacob navia

christian.bau said:
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.
 
M

Martin Wells

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
 
M

Martin Wells

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
 
K

Keith Thompson

jacob navia said:
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.
 
I

Ian Collins

Martin Wells wrote:

Please don't snip attributions.
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.
 
C

Charlie Gordon

christian.bau said:
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.
 
C

Charlie Gordon

Martin Wells said:
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.
 
K

Keith Thompson

Charlie Gordon said:
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.
 
C

Charlie Gordon

Keith Thompson said:
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 ?
 
K

Keith Thompson

Charlie Gordon said:
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++.
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.
 
C

Chris Torek

[snippage]

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.
 
J

Jack Klein

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top