Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default parameters in function call in C

Lets say in a file I have some code in two files part of the same project

file1.c
int func1(int a, int b, int c, bool d)
{
        /* function body */
}

file2.c
extern func1(int a, int b, int c);

/* function call */
func1(runtime1, runtime2, runtime3);

What would be value bool d takes when being called from file2.c? I know this is really bad practice, but I am maintaining old code and somebody did this, I just want to know the default parameter or if it is implementation dependent. Please note also, that bool in this example is a typedef of the software, since this specific project does not support C99. Thank you.!

like image 257
Pedro Perez Avatar asked Sep 07 '25 01:09

Pedro Perez


1 Answers

The value would be undefined. When calling func1, its parameters go on the stack. If you call it with 1 parameter less, the stack will be sizeof(bool) bytes short of what the process expects. This will not crash your program as your stack and your heap are "facing", but if you try to access to d, you will access to whatever value is there on the stack -> rubbish.

like image 146
Eregrith Avatar answered Sep 10 '25 01:09

Eregrith