gpt4 book ai didi

c++ - 字符串常量存储在内存中的什么位置?

转载 作者:行者123 更新时间:2023-11-27 23:59:10 26 4
gpt4 key购买 nike

<分区>

在C++程序中,常量数据存放在哪里,尤其是字符串常量?

我问是因为在以下问题中:

Why can creating a static const std::string cause an exception?

Damon 的回答,最后有以下内容:

A string_view will, contrary to a string, not allocate non-constant memory, copy constant data into that, and then pretend it's constant. Instead, it will manage a pointer directly to the constant data, and that's all.

That way, your constants are truly (not just formally) constant, there are no allocations, no possibility of exceptions, and no double memory usage. And for the most part, it still looks and smells like a string. The only notable differences being that a string_view doesn't guarantee nul-termination (but the character constant it points to does, so this is irrelevant), and the fact that it's really constant, not modifiable... which is exactly what you want.

连常量都不需要存储在内存中的某个地方吗?如果它们存储在内存中(因为内存是有限的),是否有可能因为没有更多的内存而抛出异常?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com