简体   繁体   English

在makefile中运行应用程序时出现分段错误

[英]Segmentation fault when run an application out of makefile

I had a program and I could only run it from Makefile otherwise it would give me this error: 我有一个程序,只能从Makefile运行它,否则会出现此错误:

Segmentation fault (core dumped) 分段故障(核心已转储)

But if I ran the application from Makefile it would be ok. 但是,如果我从Makefile运行该应用程序,那就没问题了。 I have simplified my code: 我简化了代码:

This is my make file: 这是我的make文件:

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

and this is my code: 这是我的代码:

#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;
}

It seem this is a problem with memory occupation when it is out of makefile but how come does it work when it is inside makefile? 似乎在makefile之外时这是内存占用的问题,但是当它在makefile中时如何工作?

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

Additional info: 附加信息:

OS: Linux (Ubuntu) 操作系统:Linux(Ubuntu)

To use armadillo matrix library: 要使用犰狳矩阵库:

sudo apt-get install libarmadillo-dev

I don't have armadillo so I cannot test your code. 我没有犰狳,所以我无法测试您的代码。 However by looking at it I'm guessing you have a stack overflow. 但是,通过查看它,我猜您有堆栈溢出。 Which is a particular time of segmentation fault. 这是分段故障的特定时间。

It is not uncommon for a segmentation fault to come and go depending on how the program is run (and the positions of the stars etc etc). 根据程序的运行方式(以及恒星的位置等),出现断层错误的情况并不少见。

Try reducing the size of your stack. 尝试减小堆栈的大小。 Try: 尝试:

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

Instead of: 代替:

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

Or if it really does have to be that size put it on the heap instead of the stack (use the new/delete). 或者,如果确实需要该大小,则将其放到堆而不是堆栈上(使用new / delete)。 High memory usage instances should not be on the stack and always on the heap. 高内存使用率实例不应位于堆栈上,而应始终位于堆上。

Use heap instead of stack: 使用堆而不是堆栈:

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

Of course, you'll need to use m1->SomeMember instead of m1.SomeMember 当然,您需要使用m1-> SomeMember而不是m1.SomeMember

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM