下面这段代码运行会crash
#include <iostream>
#include <windows.h>
int main() {
std::function<void()> fun = [=]{
std::cout << "AAA" << std::endl;
Sleep(1000);
fun();
};
fun();
return 0;
}
复制代码
把[=]改成[&] 就能通过了
#include <iostream>
#include <windows.h>
int main() {
std::function<void()> fun = [&]{
std::cout << "AAA" << std::endl;
Sleep(1000);
fun();
};
fun();
return 0;
}
复制代码
而我的函数需要用[=]方式,为啥会crash呢
fun
must be constructed first and then called, but since it is a value copy, thefun
copied whenfun
is constructed may be incomplete.If you must also copy
fun
(capture by value), you can use the delay method:There is no warning, and the program behaves as expected.
In the first piece of code, you implement the
fun()
function by copying, that is, every timefun()
is called, a copy will be made at the same time, and then another copy will be made inside the copy, and this will be repeated several times. In the future, there will be manyfun()
recursing at the same time. This is a process of geometric growth. This is a bit like thelinux
function infork
. Iffork
is written in a loop, the process coming out offork
will also be a geometric growth process. However, this copy + recursion process is still relatively complicated and difficult to explain clearly in one sentence. If you are interested, you can search forfork
related articles, which will be helpful for you to understand this issueThe second piece of code will not copy a new function, so there is only one
.fun()
recursing. This is a linear growth process (but when the recursion reaches the limit, there will still becrash
, but it takes a long time. Long, because there will be 1 second between the two recursions. If you remove theSleep()
, you should see itcrash
)