临时材料的保存期限有什么要求?
考虑下面的代码:
class Test() {
public:
Test()
{
memset( buffer, 0, sizeof( buffer ) );
}
void Process()
{
printf( buffer );
}
private:
char buffer[1000];
};
int main()
{
Test().Process();
char buffer[1000] = {};
print( buffer );
return 0;
}
我无法推断是否允许 main 中的 buffer
重用之前由 class Test
的临时对象占用的内存。根据标准自动存储 (3.7.2/1) 必须至少持续到块结束。
我找不到强制临时对象使用自动存储的措辞,除了 6.6/2 之外,其中描述了跳转语句并表示在退出作用域时 [...],调用析构函数(12.4)所有具有自动存储持续时间(3.7.2)的构造对象(命名对象或临时对象)这似乎暗示临时对象使用自动存储。
临时人员是否需要使用自动存储?上面代码中的 main
中的局部变量是否允许重用之前临时占用的内存,或者应该使用不同的存储?
Consider the following code:
class Test() {
public:
Test()
{
memset( buffer, 0, sizeof( buffer ) );
}
void Process()
{
printf( buffer );
}
private:
char buffer[1000];
};
int main()
{
Test().Process();
char buffer[1000] = {};
print( buffer );
return 0;
}
I can't deduce whether buffer
in main is allowed to reuse the memory previously occupied by the temporary object of class Test
. According to The Standard automatic storage (3.7.2/1) must persist for at least until the block ends.
I can't find phrasing that would force a temporary object to use automatic storage except 6.6/2 where a jump statement is described and says that on exit from a scope [...], destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) which seems to imply that temporaries use automatic storage.
Are temporaries required to use automatic storage? Is the local variable in main
in code above allowed to reuse the memory previously occupied by the temporary or should it use distinct storage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
临时变量的生命周期(除非绑定到 const&)延伸到完整表达式的末尾。在您的情况下,
main
中的第一行。允许编译器重用相同的内存,但是否重用是一个实现细节(即实现质量)由于您也不例外,因此
Test
临时文件属于第一类,并在评估第一行的最后一步被销毁。The lifetime of the temporary (unless bound to a
const&
) extends to the end of the full expression. In your case the first line inmain
. The compiler is allowed to reuse the same memory, but whether it does or not is an implementation detail (i.e. quality of implementation)Since you are in neither exception, the
Test
temporary falls into the first category and is destroyed as the last step of the evaluation of that first line.3.7.2/1 特别讨论了块作用域变量。这些确实具有必须持续整个块的存储空间。但是,正如您所发现的,临时变量确实具有自动存储持续时间,但不是块范围变量。 (参见 3.3.3,块作用域与名称相关联)。
3.7.2/1 specifically discusses block-scope variables. Those do have storage that must last the block. However, as you discovered, temporaries do have automatic storage duration, but are not block-scope variables. (See 3.3.3, block scope is associated with names).
语法
Test()
创建一个临时的。这与命名对象不同:命名对象具有块持续时间;它会一直存在直到区块结束。临时有表达持续时间;当它所在的表达式结束时,它将被销毁。
因此,如果您执行
Test().Process()
,Test()
临时文件将存活足够长的时间,以便Process()
完成。The syntax
Test()
creates a temporary. This is different from an object that is named:A named object has block duration; it will live until the block ends. A temporary has expression duration; it will be destroyed when the expression it is on ends.
So, if you do
Test().Process()
, theTest()
temporary will live long enough forProcess()
to finish.Test
实例一直存在,直到出现;
为止。未指定buffer
是否重用用于Test
实例的存储。 AFAIK,标准中没有任何内容阻止编译器重用该空间。The
Test
instance lives until the;
there. Whether thebuffer
reuses the storage used for theTest
instance is unspecified. AFAIK, there is nothing in the standard preventing the compiler from reusing the space.它是实现定义的。智能编译器可以通过对齐堆栈指针来优化代码,以便缓冲区可以重用内存。
It's implementation defined. A smart compiler may optimize the code by aligning the stack pointer so that the memory can be reused by
buffer
.