在 c++ 中使用函数装饰器(使用闭包)时出现意外的分段错误
Unexpected segmentation fault when using function decorator (using closure) in c++
我创建了decorator
函数来向现有函数添加功能。程序输出正确的函数指针地址以及经过的时间,以按预期迭代 10 xhelloworld
。
然而,如果我更改decorator
函数以按值(FunctionPointer original_function
)获取original_function
,则程序会因分段错误而终止,我不明白它失败的原因。
#include <iostream>
#include <chrono>
typedef void (*FunctionPointer)();
auto
decorator(FunctionPointer && original_function) // if changed to FunctionPointer original_function, it causes segmentation fault when the closure(lambda expression) is called later on
{
std::cout << "Decorator: " << (void*)original_function << std::endl; // 0x558072fb0b90
return [&]()
{
std::cout << "Decorator: " << (void*)original_function << std::endl; // 0x558072fb0b90 but 0x0 when original_function passed by value
auto t0 = std::chrono::high_resolution_clock::now();
original_function();
auto duration = std::chrono::high_resolution_clock::now() - t0;
std::cout << "nElapsed " << duration.count() * 1000.0f << " msn";
};
}
void
helloworld(void)
{
for (auto i = 0; i < 10; i++)
std::cout << "Hello, World!n";
}
int
main(void)
{
std::cout << "Main: " << (void*)helloworld << std::endl; // 0x558072fb0b90
auto my_helloworld = decorator(helloworld);
my_helloworld();
return 0;
}
不同之处在于,当您按值传递函数时,传递给 lambda 的参数是对函数参数的引用,当decorator
返回时,该参数超出了范围。 稍后调用返回的 lambda 时,将引用此超出范围的变量,即未定义的行为。
当您通过通用引用传递时,它起作用,传递给decorator
的参数是一个引用,该引用传递给 lambda。 因此,当您稍后调用 lambda 时,它仍然有效。
您可以将 lambda 更改为按值传递(使用[=]
)以使更改后的版本正常工作。
相关文章:
- 排序程序的意外错误代码
- 在 c++ 中声明向量时出现意外错误
- 在第三个 pary 函数中使用矢量引用时出现意外错误
- 创建进程失败,意外错误
- 找到printf的意外错误,该格式为0.1F,用于四舍五入数字
- 使用-PG选项构建时出现不同的意外错误
- 未声明的标识符和类型"int"从 main 调用类时出现意外错误
- 试图读取AIFF文件意外错误与指针位置等有关
- 与不完整的Cholesky预处理的共轭梯度返回特征库的意外错误
- 意外错误"no matching function for call to",需要没有参数的构造函数
- 构建 tesseract3 训练模块时出现意外错误:"not declared in this scope"
- 在 C++ 中使用关键字 "this" 时出现意外错误
- 在我的Mac OSX El Capitan上编译带有意外错误的简单C++类
- 尝试使用模板创建类的新实例,意外错误
- C++处理意外错误
- Visual Studio 中的意外错误包含文件
- 将 c++ 程序与 mysql 连接时出现意外错误
- C++运算符意外错误
- 意外错误ID -不确定原因(c++)
- 模板类中的变量模板-意外错误(可能存在错误?)