Hi everyone -
I am not quite sure to understand what is really going on when a
function defined in one translation unit calls a function defined in a
different translation unit without knowing its prototype. Let's say for
instance :
foo.c
#include <stdio.h>
void foo(float a, int b)
{
printf("%f (0x%x) %d (0x%x)\n", a, &a, b, &b);
}
main.c
int main(void)
{
float a = 5.0;
int b = 10;
foo(a, b);
return 0;
}
According to what I have read, the argument a should be promoted to
double, but the function foo expects to get a float as its first
parameter. The result is undefined as the binary representation / space
occupied by a float and a double are completely different. I'm OK with
that.
However, if I change the foo function definition to foo(char a, char b)
the arguments should be promoted to int. As foo would expect two char
as parameters the address of a and b should be *(ebp + 4) and *(ebp +
5). This is not the case, it appears that b is located sizeof(int)
deeper in the stack than a so the function displays the correct values.
I do not understand why the behavior is different ... did I miss
something related to type promotion and the circumstances under which
it occurs?
Thank you,
Yannick
I am not quite sure to understand what is really going on when a
function defined in one translation unit calls a function defined in a
different translation unit without knowing its prototype. Let's say for
instance :
foo.c
#include <stdio.h>
void foo(float a, int b)
{
printf("%f (0x%x) %d (0x%x)\n", a, &a, b, &b);
}
main.c
int main(void)
{
float a = 5.0;
int b = 10;
foo(a, b);
return 0;
}
According to what I have read, the argument a should be promoted to
double, but the function foo expects to get a float as its first
parameter. The result is undefined as the binary representation / space
occupied by a float and a double are completely different. I'm OK with
that.
However, if I change the foo function definition to foo(char a, char b)
the arguments should be promoted to int. As foo would expect two char
as parameters the address of a and b should be *(ebp + 4) and *(ebp +
5). This is not the case, it appears that b is located sizeof(int)
deeper in the stack than a so the function displays the correct values.
I do not understand why the behavior is different ... did I miss
something related to type promotion and the circumstances under which
it occurs?
Thank you,
Yannick
Comment