Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between static function and normal function in C?

Tags:

c

function

static

In our project, we have pretty big C file of around 50K lines, written in 90's. I wanted to split the file based on the functionality. But, all the functions in this file are declared as static. So, file scoped. If I split the file, then the function in file1 cannot call function in file2 and vice-versa.

But, My TL feels like that there could be memory optimization by using static functions. I wrote some sample code to see if the stacks are different for different threads. It seemed like it was. Could someone please enlighten me the difference between static function and a normal one other an file scope?

like image 737
Saran-san Avatar asked Dec 25 '22 22:12

Saran-san


1 Answers

In C, while defining a function, the static keyword has the following 2 major consequences :

  1. Prevents the function name from being exported (i.e. function does NOT have external linkage). Thus, preventing linkage / direct calls from other parts of the code.

  2. As the function is clearly marked private to the file, the compiler is in a better position to generate a complete call-graph for the function. This may result in the compiler deciding to automatically in-line the function for better performance.

like image 74
Ziffusion Avatar answered Jan 18 '23 23:01

Ziffusion