Re: sizeof operator implementation



Skarmander wrote:
Stephen Sprunk wrote:
"Chris Dollin" <chris.dollin@xxxxxx> wrote in message
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.

Let's ignore the fact that you'd better put parentheses around "val" in the
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) should
"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.

Casting 10 to a pointer type does *not* invoke UB, it invokes
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.

.



Relevant Pages

  • Re: gcc knows about malloc()
    ... While omitting the system header file is a mistake, ... speaking the resultant problems aren't caused by casting the ... the return value is assumed to be an int. ... casting the phony int result to a pointer type might happen ...
    (comp.lang.c)
  • Re: Casting the return value of malloc() ?
    ... casting the return value is unwanted. ... including stdlib.h the return value is int (or int * cant remember ... compiler knows different, which it is allowed to but not required to.) ... Since you're assigning the value returned by malloc to a pointer object, ...
    (comp.lang.c)
  • Re: Casting the return value of malloc() ?
    ... casting the return value is unwanted. ... including stdlib.h the return value is int (or int * cant remember ... compiler knows different, which it is allowed to but not required to.) ... Since you're assigning the value returned by malloc to a pointer object, ...
    (comp.lang.c)
  • Re: cast from int** to int*
    ... Oh - and whatever you think you are doing, you're not casting here... ... In C, implicit cast is there. ... You declared fun as taking an int*, ... What did your compiler tell you? ...
    (comp.lang.c)
  • Re: stat() / lstat() and incorrect field values
    ... :Casting st_dev to an int 'magically' fixes everything - I'm guessing the ... :compiler was assuming it was an int when it's actually a short, ...
    (comp.os.linux.development.apps)