当前位置:网站首页>Advanced formula 46 of C language: function and macro analysis
Advanced formula 46 of C language: function and macro analysis
2022-04-21 18:15:00 【Dongchuan Jun】
Functions and macros

Macros are directly replaced and expanded by preprocessors , The compiler does not know the existence of macros ;
Functions are entities compiled directly by the compiler , The calling behavior is determined by the compiler ;
Using macros multiple times will increase the size of the final executable program ;
The function is executed by jumping , Only one function body exists in memory ;
Macros are more efficient than functions , Because it's a direct expansion , No call overhead ;
An activity record is created when the function is called , Not as efficient as macro ;
Functions and macros , Examples are as follows
#include <stdio.h>
#define RESET(p, len) \
while( len > 0 ) \
((char*)p)[--len] = 0
void reset(void* p, int len)
{
while( len > 0 )
((char*)p)[--len] = 0;
}
int main()
{
int array[] = {1, 2, 3, 4, 5};
int len = sizeof(array);
int i = 0;
for(i=0; i<5; i++)
{
printf("array[%d] = %d\n", i, array[i]);
}
return 0;
}
Macros are slightly more efficient than functions , But its side effects are huge ;
Macros are text substitutions , Parameter type cannot be checked ;
Functions that can be completed with functions never need macros ;
Recursive definitions cannot appear in macro definitions ;
Side effects of macros , Examples are as follows
#include <stdio.h>
#define _ADD_(a, b) a + b
#define _MUL_(a, b) a * b
#define _MIN_(a, b) ((a) < (b) ? (a) : (b))
int main()
{
int i = 1;
int j = 10;
printf("%d\n", _MUL_(_ADD_(1, 2), _ADD_(3, 4)));
printf("%d\n", _MIN_(i++, j));
return 0;
}
The magic of macro
Used to generate some regular code ;
Packaging function , Add type information ;
The magic of macro , Examples are as follows
#include <stdio.h>
#include <malloc.h>
#define MALLOC(type, x) (type*)malloc(sizeof(type)*x)
#define FREE(p) (free(p), p=NULL)
#define LOG_INT(i) printf("%s = %d\n", #i, i)
#define LOG_CHAR(c) printf("%s = %c\n", #c, c)
#define LOG_FLOAT(f) printf("%s = %f\n", #f, f)
#define LOG_POINTER(p) printf("%s = %p\n", #p, p)
#define LOG_STRING(s) printf("%s = %s\n", #s, s)
#define FOREACH(i, n) while(1) { int i = 0, l = n; for(i=0; i < l; i++)
#define BEGIN {
#define END } break; }
int main()
{
int* pi = MALLOC(int, 5);
char* str = "D.T.Software";
LOG_STRING(str);
LOG_POINTER(pi);
FOREACH(k, 5)
BEGIN
pi[k] = k + 1;
END
FOREACH(n, 5)
BEGIN
int value = pi[n];
LOG_INT(value);
END
FREE(pi);
LOG_POINTER(pi);
return 0;
}
Summary
Macros and functions are not competitors ;
Macros can accept any type of parameter , Efficient , Error prone ;
The arguments to the function must be of fixed type , Slightly inefficient , It's not easy to make mistakes. ;
Macros can perform functions that functions cannot ;
版权声明
本文为[Dongchuan Jun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211810489114.html
边栏推荐
- MySQL - remote connection to non local MySQL database server, Error 1130: host 192.168.3.100 is not allowed to connect to this MySQL s
- The MySQL database in MySQL is missing
- String(一个特殊的数据类型)
- 集合之ArrayList
- Golang中Json的序列化和反序列化怎么使用
- Reptile case 01
- 泛型
- Detailed explanation of kubernetes (IV) -- kubernetes deployment based on kubedm
- 【今晚七点】metaRTC的发展和应用场景探讨
- Integer面试解析、equals与==的区别
猜你喜欢

C语言进阶第47式:递归函数分析

TCP / IP protocol

Look at how the technology house saves Xueba machine. I repaired my laptop with 10 yuan

Kubernetes详解(四)——基于kubeadm的Kubernetes部署

C语言进阶第46式:函数与宏分析

Mobile platform workplus integrated office, creating a full scene business ecosystem for enterprises

Debugging garbled code of vs2019 visual studio terminal

【NPJ|数字医药】医学影像的机器学习:方法学的失败和对未来的建议

TCP/IP协议

After reading this tutorial, you will have your own satellite (DIY full explanation)
随机推荐
Tooltip component: judge whether to display tooltip according to whether the content overflows
你已经用 SharedPrefrence 的 apply() 替换 commit() 了吗?
2022超星学习通《新伦理学》答案
Live broadcast with goods source code, different writing methods of gradient status bar
Akismet plugin tutorial WordPress prevent spam filtering plugin
【pytorch图像分类】ResNet网络结构
Kubernetes详解(四)——基于kubeadm的Kubernetes部署
国泰君安安全吗?靠谱嘛
你必须懂也可以懂的微服务系列三:服务调用
MySQL query table field default value
torch.max()的用法
【acwing】1118. 分成互质组 ***(DFS)
String(一个特殊的数据类型)
Online examination of "financial law" in the computer examination of Northwest University of Technology
【NPJ|数字医药】医学影像的机器学习:方法学的失败和对未来的建议
mysql查询表字段默认值
【Redis】 使用Redis优化省份展示数据不显示
Reptile case 01
干货 | 读懂 Appium 日志,让测试效率翻倍!
php如何去除首位数字