ujjwal wrote, On 05/06/07 11:38:
Don't thank him too much. It invokes undefined behaviour by doing
arithmetic on a null pointer meaning anything can happen, including
crashing your computer or causing it to mutate in to a badly constructed
ZX80. Also, the conversion from pointer to int is not guaranteed to be
to be a simple byte count (e.g. it is not on the Cray Vector machine)
and even if it is null pointers might convert to something other than an
int value of 0 (although an integer *constant* of zero is a special case
guaranteed to be a null pointer constant).
That's just what I can think of off the top of my head.
Both of you search the group for the repeated long discussions of this.
#define ICKY_MACRO_SIZEOF(some_type, some_size) \
{ \
some_type arr[2]; \
some_size = (char*)&arr[1] - (char*)&arr[0]; \
}
#include <stdio.h>
typedef struct foobar {
int foo;
double bar;
char barfoo[7];
} foobar_t;
int main(void)
{
size_t some_size;
ICKY_MACRO_SIZEOF(char, some_size);
printf("ICKY_MACRO_SIZEOF for char = %u\n", (unsigned) some_size);
ICKY_MACRO_SIZEOF(short, some_size);
printf("ICKY_MACRO_SIZEOF for short = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(int, some_size);
printf("ICKY_MACRO_SIZEOF for int = %u\n", (unsigned) some_size);
ICKY_MACRO_SIZEOF(long, some_size);
printf("ICKY_MACRO_SIZEOF for long = %u\n", (unsigned) some_size);
ICKY_MACRO_SIZEOF(long long, some_size);
printf("ICKY_MACRO_SIZEOF for long long = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(size_t, some_size);
printf("ICKY_MACRO_SIZEOF for size_t = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(float, some_size);
printf("ICKY_MACRO_SIZEOF for float = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(double, some_size);
printf("ICKY_MACRO_SIZEOF for double = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(long double, some_size);
printf("ICKY_MACRO_SIZEOF for long double = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(char *, some_size);
printf("ICKY_MACRO_SIZEOF for char * = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(long double *, some_size);
printf("ICKY_MACRO_SIZEOF for long double * = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(foobar_t, some_size);
printf("ICKY_MACRO_SIZEOF for foobar_t = %u\n", (unsigned)
some_size);
ICKY_MACRO_SIZEOF(foobar_t *, some_size);
printf("ICKY_MACRO_SIZEOF for foobar_t * = %u\n", (unsigned)
some_size);
return 0;
}