简体   繁体   English

C中的宏内存占用量?

[英]macro memory footprint in C?

which one costs more memory? 哪一个花费更多的内存? Macro or global variable? 宏还是全局变量?

For ex. 对于前。

#define NAME "subsystem"

const char *name = "subsystem";

And

#define AGE 28
const int age = 28;

For those two cases, do they use same memory? 对于这两种情况,它们使用相同的内存吗? or is global variable size less than macro? 还是全局变量的大小小于宏?

A macro itself consumes no space whatever in an object file. 宏本身不占用目标文件中的任何空间。 It is a shortcut or mnemonic for expressing source code . 它是表示源代码的快捷方式或助记符。 The memory required by the compiled source code expressed with the help of macros depends on a variety of things, not least the details of the macro definition and the contexts and frequencies of their use. 借助宏表示的已编译源代码所需的内存取决于多种因素,不仅取决于宏定义的详细信息以及其使用的上下文和频率。

In your particular example, the global variables consume space in the compiled object file, but the macros do not. 在您的特定示例中,全局变量会占用已编译目标文件中的空间,而宏不会。 That is an artificial result, however. 但是,那是人为的结果。 Where macros representing values are used , the values they represent do consume space in the object file. 使用表示值的宏的情况下,它们表示的值确实会占用目标文件中的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM