简体   繁体   English

简单C ++程序中的分段错误

[英]Segmentation fault in a simple C++ program

The program gives me a segmentation fault . 该程序给我一个分割错误 How can I fix this problem? 我该如何解决这个问题?

I have a makefile , fir_filter.cpp , fir_filter_mod.cpp , fir_filter.h , fir_filter_mod.h and testbench.cpp . 我有一个makefilefir_filter.cppfir_filter_mod.cppfir_filter.hfir_filter_mod.htestbench.cpp Basically I use testbench to check the result between fir_filter.cpp and fir_filter_mod.cpp . 基本上,我使用testbench来检查fir_filter.cppfir_filter_mod.cpp之间的结果。

File Makefile: 文件生成文件:

CAT_HOME = $(MGC_HOME)
TARGET = my_tb
OBJECTS = fir_filter.o fir_filter_mod.o testbench.o
DEPENDS = fir_filter.h fir_filter_mod.h
INCLUDES = -I"$(CAT_HOME)/shared/include"
DEFINES =
CXX = /usr/bin/g++
CXXFLAGS = -g $(DEFINES) $(INCLUDES)

$(TARGET) : $(OBJECTS)
    $(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJECTS)

$(OBJECTS) : $(DEPENDS)

# Phony target to remove all objects and executables
.PHONY: clean
clean:
    rm -f *.o my_tb

File shift_class.h 文件shift_class.h

#ifndef __SHIFT_CLASS__
#define __SHIFT_CLASS__

template<typename dataType, int NUM_REGS>
class shift_class{
    private:
        dataType regs[NUM_REGS];
        bool en;
        bool sync_rst;
        bool ld;
        dataType *load_data;

    public:
        shift_class(): en(true), sync_rst(false), ld(false) {}
        shift_class(dataType din[NUM_REGS]):
            en(true), sync_rst(false), ld(false) {
                load_data = din;
        }
        void set_sync_rst(bool srst) {
            sync_rst = srst;
        }
        void load(bool load_in){
            ld = load_in;
        }
        void set_enable(bool enable) {
            en = enable;
        }

        void operator << (dataType din){
            SHIFT: for(int i = NUM_REGS - 1; i >= 0; i--) {
                if(en)
                    if(sync_rst)
                        regs[i] = 0;
                    else if(ld)
                        regs[i] = load_data[i];
                    else
                        if(i == 0)
                            regs[i] = din;
                        else
                            regs[i] = regs[i - 1];
            }
        }
        dataType operator [] (int i){
            return regs[i];
        }
};
#endif

File fir_filter.h 文件fir_filter.h

void fir_filter(double *x, double *y);

File fir_filter.cpp 文件fir_filter.cpp

#include "fir_filter.h"
#include "shift_class.h"
void fir_filter(double *x, double *y) {
    const double h[4] = {0.5, 0.5, 0.5, 0.5};
    static shift_class<double, 4> regs;
    double temp = 0;
    regs << *x;
    MAX: for(int i = 0; i < 4; i++) {
        temp += h[i] * regs[i];
    }
    *y = temp;
}

File fir_filter_mod.cpp 文件fir_filter_mod.cpp

#include "fir_filter_mod.h"
#include "shift_class.h"
double fir_filter_mod(double *x) {
    const double h[4] = {0.5, 0.5, 0.5, 0.5};
    static shift_class<double, 4> regs;
    double y = 0;
    double temp = 0;
    regs << *x;
    MAX: for(int i = 0; i < 4; i++) {
        temp += h[i] * regs[i];
    }
    y = temp;
    return y;
}

File fir_filter.h 文件fir_filter.h

double fir_filter_mod(double *x);

File testbench.cpp 文件testbench.cpp

#include <iostream>
#include "fir_filter.h"
#include "fir_filter_mod.h"
using namespace std;

int main()
{
    double *x;
    double *y;
    double y_mod;

    double init = 1;
    x = &init;

    for(int j = 0; j < 5; j++) {
        fir_filter(x, y);
        y_mod = fir_filter_mod(x);
        if(*y == y_mod) {
            cout << "ERROR" << endl;
        }
        else {
            cout << y_mod << endl;
        }
    }
}

Bad idea: put a lot of print to screen code in your program. 坏主意:在程序中的屏幕代码上打印很多内容。 Then you can check what is last message from your program and try to find place where segfault occured. 然后,您可以检查程序中的最后一条消息,并尝试查找发生段错误的位置。 Also you can print debug data such as values of different variables. 您还可以打印调试数据,例如不同变量的值。

Good idea: to use debugger. 好主意:使用调试器。 It makes unnecessary implementation of bad idea. 这使得不必要的坏主意实现。

Another good idea: try to make audit of code. 另一个好主意:尝试对代码进行审核。 There a lot of analyzers of C++ code. 有很多C ++代码的分析器。 They can reveal potentially dangerous places in your program. 它们可以揭示程序中潜在的危险位置。

The error is in this place: 错误在这里:

 if(*y == y_mod) {
        cout << "ERROR" << endl;

In previous step you called void fir_filter(double *x, double *y) and it doesn't change pointer value. 在上一步中,您调用了void fir_filter(double *x, double *y) ,它不会更改指针值。 But you don't initialize y in beginning of program. 但是您不要在程序开始时初始化y。 So y is pointing to nowhere and derefencing of y makes program crash. 所以y指向无处,而y取消引用使程序崩溃。

There is one problem in this part 这部分有一个问题

int main()
{
    double *x;
    double *y;
    double y_mod;

    double init = 1;
    x = &init;

    for(int j = 0; j < 5; j++) {
        fir_filter(x, y);

in that the call to fir_filter will write the result to *y , but the pointer isn't pointing anywhere. 因为对fir_filter的调用会将结果写入*y ,但是指针没有指向任何地方。

Segmentation fault in this line of fir_filter.cpp fir_filter.cpp的这一行存在分段错误

*y = temp;

Because y is pointing to nowhere. 因为y指向无处。

If you want to get that value, just return it or use reference. 如果要获取该值,只需将其返回或使用引用即可。

What you need is a debugger. 您需要的是调试器。 Try running your program in gdb and see where the crash is occurring: 尝试在gdb中运行程序,查看崩溃发生的位置:

`gdb my_tb`

Enter the run command to start your program running. 输入run命令以启动程序运行。 You can use the bt command to see the stacktrace when the crash occurs. 发生崩溃时,可以使用bt命令查看stacktrace。

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

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