gpt4 book ai didi

c++ - 程序在哪里为抛出对象分配内存?

转载 作者:行者123 更新时间:2023-12-02 02:00:44 27 4
gpt4 key购买 nike

如果我从函数 h() 中抛出一些字符串

f()->g()->h()

这样

throw std::string("error");

并且仅在 f() 中捕获。程序在哪里为该字符串分配内存?显然它不能在h()的堆栈上分配它。如果这是 f() 的堆栈,它以什么方式知道应该分配在哪里?

最佳答案

If this is stack of f() in what way it know in what place it should allocate?

它是一个以未指定方式分配的临时对象。

来自Throwing an exception标准部分:

3 A throw-expression initializes a temporary object, called the exception object, the type of which is determined by removing any top-level cv-qualifiers from the static type of the operand of throw and adjusting the type from “array of T” or “function returning T” to “pointer to T” or “pointer to function returning T”, respectively. ...

4 The memory for the exception object is allocated in an unspecified way, except as noted in [basic.stc.dynamic.allocation]. ...

关于c++ - 程序在哪里为抛出对象分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60554333/

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