Re: sizeof operator implementation
- From: "Harald van Dijk" <truedfx@xxxxxxxxx>
- Date: 7 Dec 2006 22:06:29 -0800
Skarmander wrote:
Stephen Sprunk wrote:
"Chris Dollin" <chris.dollin@xxxxxx> wrote in messageLet's ignore the fact that you'd better put parentheses around "val" in the
news:el9dua$5ob$1@xxxxxxxxxxxxxxxxxxxxx
vijay wrote:...
if you see assembly then sizeof operator has sizeof(type) or
sizeof(variable) at compile time. How does C compiler gets value at
compiler time.?
the implementation as macro is as below
#define sizeof_op1(val) (&val +1 ) - &val // for variable ex
sizeof_op1(n)
This is homework, isn't it. Oh well.
When this "works", it will get the answer `1`. It won't work for
things that don't have addresses or, more accurately, things that
aren't legal operands of &.
Conclusion: not useful. And you didn't test it.
Wouldn't this work (for some definition of "work") if you did:
#define sizeof_op1(val) ((char*)(&val+1)-(char*)(&val))
In fact, I can't see any reason that'd even invoke UB.
expansion to avoid some of the sillier pitfalls. Even so, "sizeof_op1(char)"
is a syntax error and "sizeof_op1('0')" a constraint violation. If "f" is
declared as
void f(void);
then "sizeof_op1(&f)" and "sizeof_op1(f)" are both constraint violations (of
course, so is "sizeof f"). If "a" is declared as
int a[4];
then "sizeof_op1(a[4])" yields UB.
In C99, an expression not involving sizeof can never return correct results
for variable-sized arrays.
Are you saying that
#include <stdio.h>
int main(void) {
int n = 4;
int vla[n];
printf("%td\n", (char *) (&vla + 1) - (char *) (&vla));
}
is not guaranteed to print sizeof(vla)? If so, it is. Arithmetic on
pointers to VLAs behaves the same as ordinary pointer arithmetic.
The same trick as above (casting to char* before subtracting) shouldCasting 10 to a pointer type does *not* invoke UB, it invokes
"work" for this as well. However, it definitely invokes UB due to
casting 10 to a pointer type; might as well cast 0 and make what you're
doing obvious.
implementation-defined behavior. Conversions of pointers to and from
integers are perfectly legal. What will most likely invoke UB is adding 1 to
the result, since it will probably not point to an object.
The standard says casting an integer to a pointer type may result in a
trap representation. This is not possible, since merely casting an
integer to a pointer type does not result in an object, and if there is
no object, there is no representation. The intent may or may not be
that simply casting 10 to a pointer type is allowed to result in
undefined behaviour, the standard is not clear. I'd say both Stephen
Sprunk's interpretation and yours are valid.
.
- References:
- sizeof operator implementation
- From: vijay
- Re: sizeof operator implementation
- From: Chris Dollin
- Re: sizeof operator implementation
- From: Stephen Sprunk
- Re: sizeof operator implementation
- From: Skarmander
- sizeof operator implementation
- Prev by Date: Re: I have a doubt
- Next by Date: Re: I have a doubt
- Previous by thread: Re: sizeof operator implementation
- Next by thread: passing float to function
- Index(es):
Relevant Pages
|