N
Nudge
Hello everyone,
Does the following code invoke undefined behavior?
In other words, is it dangerous to lie to the compiler about a
function's return type, when the return type is the same size or
smaller than sizeof(int).
It seems to me there could be platforms where
char = 8 bits, short = 16 bits, int = 32 bits,
and, on that hypothetical platform, chars, shorts, and ints
might be stored in different registers altogether?
int printf(const char *format, ...);
typedef int fp_t(void);
int f1(void)
{
register int res = -1;
return res;
}
short f2(void)
{
register short res = -2;
return res;
}
signed char f3(void)
{
register signed char res = -3;
return res;
}
void foo(fp_t fp)
{
printf("%d\n", fp());
}
int main(void)
{
foo(f1);
foo((fp_t *)f2);
foo((fp_t *)f3);
return 0;
}
Does the following code invoke undefined behavior?
In other words, is it dangerous to lie to the compiler about a
function's return type, when the return type is the same size or
smaller than sizeof(int).
It seems to me there could be platforms where
char = 8 bits, short = 16 bits, int = 32 bits,
and, on that hypothetical platform, chars, shorts, and ints
might be stored in different registers altogether?
int printf(const char *format, ...);
typedef int fp_t(void);
int f1(void)
{
register int res = -1;
return res;
}
short f2(void)
{
register short res = -2;
return res;
}
signed char f3(void)
{
register signed char res = -3;
return res;
}
void foo(fp_t fp)
{
printf("%d\n", fp());
}
int main(void)
{
foo(f1);
foo((fp_t *)f2);
foo((fp_t *)f3);
return 0;
}