[英]How memory is allocated to macros in c?
I would like to know how the memory is allocated to #define variables in C. 我想知道如何将内存分配给C中的#define变量。
#define VAR1 10
I have 2 questions... 我有两个问题......
In which memory segment VAR1 is stored?
存储段VAR1存储在哪?
In none of the segment. 在所有部分中都没有。
VAR1
is relevant only in pre-processing stage and does not have any identity at run time. VAR1
仅在预处理阶段相关,并且在运行时没有任何标识。 During pre-processing all instances of VAR1
are replaced with 10
so there is no memory requirement at run time because 10
is an integer literal. 在预处理期间,
VAR1
所有实例都替换为10
因此在运行时没有内存要求,因为10
是整数文字。
What's the type of VAR1?
VAR1的类型是什么?
VAR1
is replaced with 10
at pre-processing stage. VAR1
在预处理阶段被替换为10
。 10
being an integer literal
, we can say type or VAR1
is int
. 10
是integer literal
,我们可以说类型或VAR1
是int
。
Moral: Macros are not variables. 道德:宏不是变量。
To my understanding, a definition via a macro does neither have a type nor explicitly allocates memory; 根据我的理解,通过宏的定义既没有类型也没有显式分配内存; the right-hand side of the definition (
10
in this case) is expanded textually into any occurence of the left-hand side ( VAR1
in this case) before the compilation. 在编译之前,定义的右侧(在这种情况下为
10
在文本上扩展到左侧(在这种情况下为VAR1
)的任何出现。
Macros
are not variables
. Macros
not variables
。 They are just a common name for some value. 它们只是某些价值的通用名称。 In your case,
VAR1
corresponds to integer
value 10
. 在您的情况下,
VAR1
对应于integer
数值10
。
Macro is not stored anywhere in the memory. 宏未存储在内存中的任何位置。 When we compile the program in C or C++, it is done in many stages.
当我们用C或C ++编译程序时,它在很多阶段完成。 First, the syntax is checked.
首先,检查语法。 If syntax is correct, it is checked for semantic errors.
如果语法正确,则检查语义错误。 If it passes then, the
.c
program file is converted into Object code
. 如果它通过,则
.c
程序文件将转换为Object code
。 During this conversion, the preprocessors are processed ie the header files are included, any external linked file is included and all the macro are replaced with their corresponding values
(in your case, at any place the program finds VAR1
, it will replace that with value 10
). 在此转换期间,处理预处理器,即包含头文件,包含任何外部链接文件,并将
all the macro are replaced with their corresponding values
(在您的情况下,在程序找到VAR1
任何位置,它将用值替换它10
)。
After this phase, all the code has already been converted to nearly machine level code. 在此阶段之后,所有代码都已转换为接近机器级代码。
I hope you got your answer. 我希望你得到你的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.