Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can functions used through function pointers be inlined?

Tags:

c

#include <stdlib.h>

inline int f0(int a) {
  return a*a;
}

inline int f1(int a) {
  return a*a*a;
}

int main() {
  int (*f)(int);
  f = rand()%2 ? f0 : f1;
  return f(rand());
}

So with gcc, asm file generated is same with or without inline. Is it same with any code with function pointers?

like image 681
anon18274612 Avatar asked Sep 04 '11 20:09

anon18274612


1 Answers

Function pointers cannot be inlined unless their value is fully decidable at compile time. Your case is not decidable.

Most of the time function pointers will never be inlined, even if the compiler can see which function is in the function pointer.

like image 50
Yann Ramin Avatar answered Sep 28 '22 08:09

Yann Ramin