C 中常量占用多少内存?

发布于 2024-08-10 01:17:46 字数 99 浏览 4 评论 0原文

这样做时:

const int a = 5;

我想知道 a 是否会像变量一样获得 4 字节的内存? (32位系统下)

when doing like this:

const int a = 5;

I wonder if a will get 4-byte of memory just like a variable ? (in 32 bit system)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

缱倦旧时光 2024-08-17 01:17:46

是的,会的。
尽管如果您从不获取它的地址,那么优化器很可能会完全删除它,并在您的情况下用数字 5 替换对常量的任何引用。

Yes it will.
Although if you never take it's address then the optimizer might well remove it entirely and just replace any references to the constant with the number 5 in your case.

守护在此方 2024-08-17 01:17:46

这取决于。

const int a = 5;

将占用四个字节的内存(或者 int 在您的系统上占用多少字节)。

如果将其设为静态:

static const int a = 5;

那么优化器可以自由地将 a 的每个实例替换为值 5。优化器在第一种(非静态)情况下无法做到这一点,因为您可能会参考a 在一个单独的编译单元中:

extern const int a;

It depends.

const int a = 5;

Will take four bytes of memory (or however many bytes an int takes up on your system).

If you make it static:

static const int a = 5;

Then the optimizer is free to replace each instance of a with the value of 5. The optimizer cannot do that in the first (non-static) case simply because you may refer to a in a separate compilation unit with:

extern const int a;
腹黑女流氓 2024-08-17 01:17:46

这取决于编译器。

例如:

const int a = 4;

这可以通过编译器分配 4 个字节并仅强制执行不变性来处理。

如果您有一个常量字符串:

static final java.lang.String name = "Foobar";

编译器可以删除该变量并将其替换为使用该变量的任何地方的实际字符串“Foobar”。这不会占用堆空间,但它仍然必须存储在程序数据段中的某个位置。当 Java 发现在多个地方使用的带引号的字符串时,它会尝试执行此操作,因此它只需存储该字符串的一份副本。

无论哪种方式,常量都不会消除存储分配。他们充其量只能最大限度地减少所需的存储空间。

It depends on the compiler.

For example:

const int a = 4;

This could be handled by the compiler allocating 4 bytes and just enforcing immutability.

If you had a constant string:

static final java.lang.String name = "Foobar";

The compiler could remove the variable and replace it with the actual string "Foobar" everywhere the variable is used. This doesn't take space from the heap but it still has to be stored somewhere in the programs data segment. Java tries to do this when it finds a quoted string that is being used in multiple places, so it only has to store one copy of it.

Either way, constants don't eliminate storage allocation. At best they can only minimize the storage needed.

流绪微梦 2024-08-17 01:17:46

这取决于你的架构,但是,无论你是否将某些东西设置为常量,并不会真正影响它的大小,而是会影响它在内存中的位置。现在,有一些编译器优化可能会改变您认为实际会发生的情况,但这是基本思想。

It depends on your architecture but yes whether you make something const or not does not really affect its size but more its location in memory. Now, there are some compiler optimizations that may change what you think will actually happen but this is the basic idea.

趁年轻赶紧闹 2024-08-17 01:17:46

int aconst int a 之间的内存消耗没有区别。

但请注意,在 C 中,声明为 const 的对象不会形成常量表达式(与 C++ 不同),并且默认情况下具有外部链接(再次与 C++ 不同)。所有这些意味着在 C 中,常量对象与非常量对象几乎相同,只是不可修改。

此外,这意味着在 C 中,常量对象几乎没有机会被“删除”,正如其他答案声称的那样。如果您确实希望它在 C 中“可移动”,则必须显式将其声明为static。但即使这样也不会生成 const int 对象来形成常量表达式,即您仍然不能使用它来指定 C89/90 中的数组大小,而在 C99 中,结果数组仍然是一个变量 -长度数组 (VLA)。

There's no difference in memory consumption between int a and const int a.

Note though, that in C objects declared as const don't form constant expressions (as opposed to C++) and have external linkage by default (as opposed to C++, again). All this means that in C a constant object is pretty much the same thing as a non-constant object, just non-modifiable.

Also, it means that in C a constant object has very little chance to get "removed", as other answers claim it will. If you really want it to make it "removable" in C, you have to declare it as static explicitly. But even that won't make a const int object to form constant expressions, i.e. you still can't use it to designate array size in C89/90 and in C99 the resultant array is still a variable-length array (VLA).

青柠芒果 2024-08-17 01:17:46

常量变量需要 4 个字节的内存,但如果它是一个值,则需要 0 个字节,因为汇编代码将像这样嵌入该值

mov eax, 5

这里 5 不是来自变量,而是常量 5,并且它甚至会生成更快的代码,因为不需要内存调用来检索值,它只是汇编代码的一部分

a constant variable requires 4 bytes of memory, but if it is a value it requires 0 bytes since the assembly code will embbed the value like this

mov eax, 5

here 5 don´t comes from a variable but it is the constant 5, and it will even generate faster code since no memory call are required to retrieve the value, it is just part of the assembly code

白况 2024-08-17 01:17:46

它可能需要通常的数量,但如果您只以不需要它有地址的方式使用它,编译器/链接器可能会优化它,因此它根本不占用任何内存。

It might take the usual amount, but if you only use it in ways that never require it to have an address, the compiler/linker may optimize it away so it doesn't occupy any memory at all.

恰似旧人归 2024-08-17 01:17:46

通常,常量将占用与变量相同的空间,因此如果 int 在您的体系结构上是 32 位,则 a 也将占用 32 位。
然而,编译器也可能决定直接将常量放入代码中,根本不为常量本身分配空间。这将取决于常量实际定义的位置,这意味着编译器是否能够确定没有机会修改 a (例如通过 const 强制转换)或获取 a 的地址。

Generally the constant will take the same space as a variable, so if int is 32bit on your architecture, a will take 32bit as well.
However the compiler might also decide to directly put the constant into the code, not assining space for the constant itself at all. This will depend on where the constant is actually defined, meaning if the compiler is able to determine that there is no chance to either modifiy a (e.g. through const casts) or take the address of a.

彼岸花ソ最美的依靠 2024-08-17 01:17:46

在嵌入式系统中,只读存储器与可写存储器是分开的,该常量不会占用RAM,而是仅存储在ROM中。同样,在具有虚拟内存的系统上,常量将被加载到只读内存页中,并且无论有多少个正在运行的程序副本,都只会占用 RAM 一次。

On an embedded system, where read-only memory is separate from writable memory, this constant will take up no RAM, it will be stored only in ROM. Likewise, on a system with virtual memory, constants will get loaded into read-only memory pages and will only take up RAM once no matter how many running copies of the program there are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文