编译时内联内存分配
Inline allocation of memory at compile time
C/C++ 有多种方式在编译时分配内存。例如,我可以添加一个全局或静态变量。存储值的内存在编译时分配:
int x;
// -- or --
void f() {
static int y;
}
// -- or --
class C {
static int z;
};
int C::z;
但是这个解决方案不能用于分配内存"inline"。有没有可能在一行中做类似下面的事情?
static int value_behind_x; // does not need to be "named"
int * const x = &value_behind_x;
我的意思是这样的:
int * const x = static_new int;
// -- or --
int * const x_array = static_new int[10];
我知道 C/C++ 中的一件事允许这样的事情:字符串文字。但是,它们必须是常量,并且不允许将大小指定为数字。
如果没有这样的方式,是否有这样的原因或将来可能会实施?这会很好,因为它可以实现 constexpr
版本的容器,例如 std::vector
.
首先,具有静态持续时间的变量的内存在编译时并没有真正分配。它在链接时甚至在启动时分配。虽然差异很小,但在某些情况下很重要。
其次,您正在寻找的似乎是一种分配具有自动或静态持续时间的数据块的方法。传统上,它是使用 C-style 数组、std::array
完成的,并且在可用时使用 VLA 扩展作为 VLA 数组。
static
将:
Allocate memory at compile time
这是一种误解。 Static Storage Durration 定义:
The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with static
or extern
.
了解静态存储持续时间对于对抗 "static initialization fiasco"。
至关重要
通过这种理解,我们可以看到构造一个指向变量的 static
指针没有性能优势,例如 static unique_ptr<const int> x(new int(13));
而只是使用必要时value_behind_x
的地址是可取的。
同样的事情也适用于分配具有静态存储持续时间的数组。您可以使用 array
if you need container encapsulation of your array: static array<int, 10> x_array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
But generally speaking with the improvements to range access and container access provided by c++11, c++14, and c++17 in the iterator library 之类的容器 我只是建议您坚持使用:
static const x_array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
编辑:
请注意,string literals also have Static Storage Duration, meaning that thy are not "allocated at compile time." In fact There are only 3 storage durations 除了 C++ 使用的静态存储持续时间:
- Automatic Storage Duration. The object is allocated at the beginning of the enclosing code block and deallocated at the end. All local objects have this storage duration, except those declared
static
, extern
or thread_local.
- Thread Storage Duration. The object is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the object. Only objects declared thread_local have this storage duration. thread_local can appear together with
static
or extern
to adjust linkage.
- Dynamic Storage Duration. The object is allocated and deallocated per request by using dynamic memory allocation functions.
所以不,运行前没有分配或初始化。我也不希望出现这种情况,因为这需要基本的 C++ 意识形态改变 和 某种程度上的计算机体系结构改变。
请求的功能已部分安排在 C++20 中。目前只允许临时分配。
这意味着new
在编译时有效。但是,编译时分配的内存目前不允许泄漏到运行时。可以在 P0784 中找到此限制背后的原因。未来版本可能会添加对非瞬态分配的支持。
C/C++ 有多种方式在编译时分配内存。例如,我可以添加一个全局或静态变量。存储值的内存在编译时分配:
int x;
// -- or --
void f() {
static int y;
}
// -- or --
class C {
static int z;
};
int C::z;
但是这个解决方案不能用于分配内存"inline"。有没有可能在一行中做类似下面的事情?
static int value_behind_x; // does not need to be "named"
int * const x = &value_behind_x;
我的意思是这样的:
int * const x = static_new int;
// -- or --
int * const x_array = static_new int[10];
我知道 C/C++ 中的一件事允许这样的事情:字符串文字。但是,它们必须是常量,并且不允许将大小指定为数字。
如果没有这样的方式,是否有这样的原因或将来可能会实施?这会很好,因为它可以实现 constexpr
版本的容器,例如 std::vector
.
首先,具有静态持续时间的变量的内存在编译时并没有真正分配。它在链接时甚至在启动时分配。虽然差异很小,但在某些情况下很重要。
其次,您正在寻找的似乎是一种分配具有自动或静态持续时间的数据块的方法。传统上,它是使用 C-style 数组、std::array
完成的,并且在可用时使用 VLA 扩展作为 VLA 数组。
static
将:
Allocate memory at compile time
这是一种误解。 Static Storage Durration 定义:
The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with
static
orextern
.
了解静态存储持续时间对于对抗 "static initialization fiasco"。
至关重要通过这种理解,我们可以看到构造一个指向变量的 static
指针没有性能优势,例如 而只是使用必要时static unique_ptr<const int> x(new int(13));
value_behind_x
的地址是可取的。
同样的事情也适用于分配具有静态存储持续时间的数组。您可以使用 array
if you need container encapsulation of your array: But generally speaking with the improvements to range access and container access provided by c++11, c++14, and c++17 in the iterator library 之类的容器 我只是建议您坚持使用:static array<int, 10> x_array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
static const x_array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
编辑:
请注意,string literals also have Static Storage Duration, meaning that thy are not "allocated at compile time." In fact There are only 3 storage durations 除了 C++ 使用的静态存储持续时间:
- Automatic Storage Duration. The object is allocated at the beginning of the enclosing code block and deallocated at the end. All local objects have this storage duration, except those declared
static
,extern
or thread_local.- Thread Storage Duration. The object is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the object. Only objects declared thread_local have this storage duration. thread_local can appear together with
static
orextern
to adjust linkage.- Dynamic Storage Duration. The object is allocated and deallocated per request by using dynamic memory allocation functions.
所以不,运行前没有分配或初始化。我也不希望出现这种情况,因为这需要基本的 C++ 意识形态改变 和 某种程度上的计算机体系结构改变。
请求的功能已部分安排在 C++20 中。目前只允许临时分配。
这意味着new
在编译时有效。但是,编译时分配的内存目前不允许泄漏到运行时。可以在 P0784 中找到此限制背后的原因。未来版本可能会添加对非瞬态分配的支持。