1

I am using an older version of the Diab C compiler.

In my code I have taken a function name and redefined it as a function pointer with the same signature. Before making this change the code worked. After the change it made it caused the embedded system to lock up.

The function pointer was declared extern in a header, defined in one .c file, and used in another .c file. When it was called from the second .c file it would cause the system to lock up. When I attempted to add debug information using sprintf it finally told me that it was an undefined symbol. I realized that the header file was was not included in the second .c file. When I #included it everything compiled and worked correctly.

My question is, is there some C rule that allowed the compiler to deduce the function signature even though the symbol was undefined at the call location? To my understanding there should have been an error long before I made any changes.

Graznarak
  • 3,626
  • 4
  • 28
  • 47
  • 1
    IMO strongly related - http://stackoverflow.com/questions/6488429/implicit-declaration-of-function – fvu Feb 22 '13 at 00:18

2 Answers2

1

If no declaration is available, the compiler uses a default declaration of a function taking an unknown number of arguments and returning an int. If you turn up compiler warnings (eg -Wall -Wextra -Werror with gcc, check the documentation for your compiler), you should get a compile time warning.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 1
    I think this behaviour was a legacy of K&R C and removed in C99: http://stackoverflow.com/questions/5885156/no-defined-type-of-a-function-parameter-defaults-to-int-am-i-insane – congusbongus Feb 22 '13 at 00:20
1

Most likely, the code at first worked because it was compiled in the C89 or similar mode. The C standard from 1989 allows calling functions without first declaring them.

When you changed the code to use a pointer but didn't include the declaration of the pointer, the compiler assumed that your pointer was in fact a function and generated code to call into the pointer, as if the pointer had executable code inside. As the result, the program understandably stopped working.

What you should do is enable all possible warnings (for gcc: -Wall, -Wextra and make sure optimization is enabled (-O2 is good) because it enables code analysis), especially for calling functions without prototypes. A better thing might be to switch the compiler into the C99 mode (-std=c99 in gcc) or switch to a C99 compiler. The C standard from 1999 prohibits calling functions without prototypes and comes with some useful features absent in C89.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • The problem is that I am using an older version of the Diab compiler. When I say older, I mean at LEAST 10 years, but possibly closer to 20. – Graznarak Feb 22 '13 at 00:50
  • 1
    Then you need a different compiler. Btw, you could compile platform-independent parts of your code with a better compiler and see if it catches any problems. Compiling code with several different compilers is useful. – Alexey Frunze Feb 22 '13 at 01:20