M
Myth__Buster
Hi,
On a Linux system with gcc, I am just wondering why sizeof(main) can
be 1 or sizeof when applied on any function name can yield 1 ever? Or
is it only gcc's perspective to say sizeof of an implicit function
pointer to be 1 since it gives sizeof(void) to be 1 based on the
backward compatibility with the pre C99 notion that void* had its
predecessor char* and usually sizeof(char) being 1?
Also, I tried the invariably buggy code to see if at all sizeof(main)
= 1 can be realized.
/* Keep the warnings aside for a minute, please! */
#include <stdio.h>
int main(void)
{
printf("sizeof(char) : %zd\n", sizeof(char));
printf("sizeof(main) : %zd\n", sizeof(main));
char p = main; // Truncates into only one byte of the bytes
required to hold
// main's address - known bug. But just
to see sizeof(main) = 1 if at all makes any sense.
((int (*)(void))p)(); // Known to be buggy and wrong.
return 0;
}
Any explanation is cheerful.
Cheers.
On a Linux system with gcc, I am just wondering why sizeof(main) can
be 1 or sizeof when applied on any function name can yield 1 ever? Or
is it only gcc's perspective to say sizeof of an implicit function
pointer to be 1 since it gives sizeof(void) to be 1 based on the
backward compatibility with the pre C99 notion that void* had its
predecessor char* and usually sizeof(char) being 1?
Also, I tried the invariably buggy code to see if at all sizeof(main)
= 1 can be realized.
/* Keep the warnings aside for a minute, please! */
#include <stdio.h>
int main(void)
{
printf("sizeof(char) : %zd\n", sizeof(char));
printf("sizeof(main) : %zd\n", sizeof(main));
char p = main; // Truncates into only one byte of the bytes
required to hold
// main's address - known bug. But just
to see sizeof(main) = 1 if at all makes any sense.
((int (*)(void))p)(); // Known to be buggy and wrong.
return 0;
}
Any explanation is cheerful.
Cheers.