gpt4 book ai didi

c++ - 在 makefile 之外运行应用程序时出现段错误

转载 作者:行者123 更新时间:2023-11-28 06:18:49 25 4
gpt4 key购买 nike

我有一个程序,我只能从 Makefile 运行它,否则它会给我这个错误:

Segmentation fault (core dumped)

但是如果我从 Makefile 运行应用程序就没问题了。我简化了我的代码:

这是我的制作文件:

all: 
g++ aaa.cpp
run:
./a.out

这是我的代码:

#include <iostream>
#include <armadillo>

class CModel
{
public:
arma::mat::fixed<5,10000000> buffer;
};


int main()
{
CModel m1, m2;
std::cout<<"run successfully"<<std::endl;
return 0;
}

在makefile之外好像是内存占用的问题,但是在makefile里面怎么就可以呢?

$ make
g++ aaa.cpp
$ make run
./a.out
run successfully
$ ./a.out
Segmentation fault (core dumped)

附加信息:

操作系统:Linux (Ubuntu)

要使用 Armadillo 矩阵库:

sudo apt-get install libarmadillo-dev

最佳答案

我没有 Armadillo ,所以无法测试您的代码。但是通过查看它我猜你有堆栈溢出。这是段错误的特定时间。

根据程序的运行方式(以及星星的位置等),段错误的出现和消失并不少见。

尝试减少堆栈的大小。尝试:

arma::mat::fixed<5,1000> buffer;

代替:

arma::mat::fixed<5,10000000> buffer;

或者如果它确实必须是那个大小,则将它放在堆上而不是堆栈上(使用新建/删除)。高内存使用率实例不应在堆栈上,而应始终在堆上。

关于c++ - 在 makefile 之外运行应用程序时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29691972/

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