在C语言中的宏(#定义) [英] A macros in the C language (#define)

查看:258
本文介绍了在C语言中的宏(#定义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读囤积内存分配器来源$ C ​​$ c和gnuwrapper.cpp的文件中,有以下code

 的#define CUSTOM_MALLOC(x)的CUSTOM_ preFIX(的malloc)(X)

什么是 CUSTOM_ preFIX的含义(的malloc)(X)?为 CUSTOM_ preFIX 函数?但作为一个功能它没有任​​何地方所定义。如果它是可变的,那么怎样才能使用的变量,比如 VAR(的malloc)(X)

更多code:

 的#ifndef __GNUC__
#ERROR这个文件需要GNU编译器。
#万一#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&malloc.h所GT;
的#ifndef CUSTOM_ preFIX ==>这里看起来就像是一个变量,因此,如果没有定义,然后定义在这里。
#定义CUSTOM_ preFIX
#万一#定义CUSTOM_MALLOC(X)CUSTOM_ preFIX(的malloc)(X)===>什么这是什么意思?
#定义CUSTOM_FREE(X)CUSTOM_ preFIX(免费)(X)
#定义CUSTOM_REALLOC(X,Y)CUSTOM_ preFIX(realloc的)(X,Y)
#定义CUSTOM_MEMALIGN(X,Y)CUSTOM_ preFIX(memalign可)(X,Y)


解决方案

在您的code,因为CUSTOM_ preFIX被定义为没有什么,字符串 CUSTOM_ preFIX(的malloc)(X)将扩大到

 (的malloc)(X)

这相当于通常的

 的malloc(X)

不过,CUSTOM_ preFIX允许开发者选择不同的内存管理功能。例如,如果我们定义

 的#define CUSTOM_ preFIX(六)我_ ##˚F

然后 CUSTOM_ preFIX(的malloc)(X)将扩大至

  my_malloc(X)

I am reading source code of hoard memory allocator, and in the file of gnuwrapper.cpp, there is the following code

#define CUSTOM_MALLOC(x)     CUSTOM_PREFIX(malloc)(x)  

What's the meaning of CUSTOM_PREFIX(malloc)(x)? is CUSTOM_PREFIX a function? But as a function it didn't defined anywhere. If it's variable, then how can we use variable like var(malloc)(x)?

More code:

#ifndef __GNUC__
#error "This file requires the GNU compiler."
#endif

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>


#ifndef CUSTOM_PREFIX   ==> here looks like it's a variable, so if it doesn't define, then define here.
#define CUSTOM_PREFIX
#endif

#define CUSTOM_MALLOC(x)     CUSTOM_PREFIX(malloc)(x)    ===> what's the meaning of this?
#define CUSTOM_FREE(x)       CUSTOM_PREFIX(free)(x)
#define CUSTOM_REALLOC(x,y)  CUSTOM_PREFIX(realloc)(x,y)
#define CUSTOM_MEMALIGN(x,y) CUSTOM_PREFIX(memalign)(x,y)

解决方案

In your code, since CUSTOM_PREFIX is defined to be nothing, the string CUSTOM_PREFIX(malloc)(x) will expand to

(malloc)(x)

which is equivalent to the usual

malloc(x)

However, the CUSTOM_PREFIX allows the developer to choose a different memory management function. For example, if we define

#define CUSTOM_PREFIX(f) my_##f

then CUSTOM_PREFIX(malloc)(x) will be expanded to

my_malloc(x)

这篇关于在C语言中的宏(#定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆