开发者

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. Basically I use testbench to check the result between fir_filter.cpp and fir_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

#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开发者_Python百科 regs[i];
        }
};
#endif

File fir_filter.h

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

File 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

#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

double fir_filter_mod(double *x);

File 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. 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. But you don't initialize y in beginning of program. So y is pointing to nowhere and derefencing of y makes program crash.


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.


Segmentation fault in this line of fir_filter.cpp

*y = temp;

Because y is pointing to nowhere.

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 my_tb`

Enter the run command to start your program running. You can use the bt command to see the stacktrace when the crash occurs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜