[英]C language. Declaration of the array, size of which is given by the macros #define
[英]A macros in the C language (#define)
我正在閱讀囤積內存分配器的源代碼,並在gnuwrapper.cpp的文件中,有以下代碼
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x)
CUSTOM_PREFIX(malloc)(x)
的含義是什么? CUSTOM_PREFIX
是一個函數嗎? 但作為一個功能,它沒有在任何地方定義。 如果它是變量,那么我們如何使用變量如var(malloc)(x)
?
更多代碼:
#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)
在您的代碼中,由於CUSTOM_PREFIX被定義為空,字符串CUSTOM_PREFIX(malloc)(x)
將擴展為
(malloc)(x)
這相當於平常
malloc(x)
但是,CUSTOM_PREFIX允許開發人員選擇不同的內存管理功能。 例如,如果我們定義
#define CUSTOM_PREFIX(f) my_##f
那么CUSTOM_PREFIX(malloc)(x)
將擴展為
my_malloc(x)
CUSTOM_PREFIX
被定義為無,因此它將消失,留下(malloc)(x)
,這與malloc(x)
相同。 為什么? 我不知道。 也許代碼中的其他地方將CUSTOM_PREFIX
設置為其他內容。
猜測,它是一個宏,它將對malloc(x)等的調用更改為:
DEBUG_malloc( x );
您可以選擇自己提供宏,為函數提供自定義前綴,或者不在何種情況下不更改名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.