临时材料的保存期限有什么要求?

发布于 2024-11-30 09:29:38 字数 720 浏览 4 评论 0原文

考虑下面的代码:

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 技术交流群。

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

发布评论

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

评论(5

猛虎独行 2024-12-07 09:29:38

临时变量的生命周期(除非绑定到 const&)延伸到完整表达式的末尾。在您的情况下,main 中的第一行。允许编译器重用相同的内存,但是否重用是一个实现细节(即实现质量

12.2 [类.临时]

/3 [...] 临时对象在评估的最后一步被销毁
(词法上)包含它们创建点的完整表达式 (1.9)。[...]

/4 有两种上下文,其中临时变量在与完整表达式末尾不同的点被销毁。第一个上下文是当表达式作为定义对象的声明符的初始值设定项出现时。 [...]

/5 第二个上下文是当引用绑定到临时对象时。

由于您也不例外,因此 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 in main. The compiler is allowed to reuse the same memory, but whether it does or not is an implementation detail (i.e. quality of implementation)

12.2 [class.temporary]

/3 [...] Temporary objects are destroyed as the last step in evaluating
the full-expression (1.9) that (lexically) contains the point where they were created.[...]

/4 There are two contexts in which temporaries are destroyed at a different point than the end of the full expression. The first context is when an expression appears as an initializer for a declarator defining an object. [...]

/5 The second context is when a reference is bound to a temporary.

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.

被你宠の有点坏 2024-12-07 09:29:38

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).

岁月染过的梦 2024-12-07 09:29:38

语法 Test() 创建一个临时的。这与命名对象不同:

Test iHaveAName;

命名对象具有块持续时间;它会一直存在直到区块结束。临时有表达持续时间;当它所在的表达式结束时,它将被销毁。

因此,如果您执行 Test().Process()Test() 临时文件将存活足够长的时间,以便 Process() 完成。

The syntax Test() creates a temporary. This is different from an object that is named:

Test iHaveAName;

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(), the Test() temporary will live long enough for Process() to finish.

眼波传意 2024-12-07 09:29:38

Test 实例一直存在,直到出现 ; 为止。未指定 buffer 是否重用用于 Test 实例的存储。 AFAIK,标准中没有任何内容阻止编译器重用该空间。

The Test instance lives until the ; there. Whether the buffer reuses the storage used for the Test instance is unspecified. AFAIK, there is nothing in the standard preventing the compiler from reusing the space.

追风人 2024-12-07 09:29:38

它是实现定义的。智能编译器可以通过对齐堆栈指针来优化代码,以便缓冲区可以重用内存。

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.

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