ITPub博客

首页 > 应用开发 > IT综合 > C/C++ inline 函数

C/C++ inline 函数

原创 IT综合 作者:gaopengtttt 时间:2016-06-01 17:38:28 0 删除 编辑


C/C++中包含了一类inline函数,其只是单纯在原本函数申明或者定义前面多了一个inline
但是带来含义的确实不一样的。
如果不带inline那么主函数执行到函数入口处会跳到相应的函数代码除继续执行,在内存
中的代码段内存中这些代码不是连续的,这样肯能带来一些时间损耗
如果加入inline后函数会copy一份到主函数中,这样占用一定的内存但是不会jump(应该汇编使用的jump指令)


那么这样一来,可能inline函数适用的范围为函数代码本身代码量很少,而且执行非常快。
如果代码量大那么COPY占用的内存过多,如果执行非常慢,减少JUMP带来的提升只是
微不足道的提升,下面演示他的使用
  
  
  以下的列子为了展示3个问题
  1、inline function 申明
  2、使用typedef 定义一个函数指针的别名,并且使用它来声明一个f_p的变量接受add的地址
  3、函数返回的const类型的指针必须和在主函数中使用const int *接受
  
  1 /*************************************************************************
  2     > File Name: inline.cpp
  3     > Author: gaopeng
  4     > Mail: gaopp_200217@163.com 
  5     > Created Time: Thu 26 May 2016 09:45:18 PM CST
  6  ************************************************************************/
  7 
  8 #include
  9 
 10 typedef  const int* (*Fun_p)(const int *input);//typedef define a Fun_p alias to a function pointer 
 11 using namespace std;
 12 
 13 inline const int * add(const  int *input);
 14 int main(void)
 15 {
 16     int input = 2;
 17     const int *re;
 18     Fun_p  f_p = add;
 19     re = f_p(&input);
 20     cout<< *re <<endl;
 21 
 22 }
 23 
 24 
 25 
 26 inline const int * add(const  int *input)
 27 {   
 28     static int addva; 
 29     addva = *input+*input;
 30     return &addva;
 31 
 32 }         
</endl;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7728585/viewspace-2112427/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
wxh gp_22389860 公众号 MySQL学习 书籍 深入理解MySQL主从原理

注册时间:2021-06-07

  • 博文量
    868
  • 访问量
    3495592