[英]A simple C++ shared memory program written on linux: segmentation fault
[英]Segmentation fault in a simple C++ program
該程序給我一個分割錯誤 。 我該如何解決這個問題?
我有一個makefile
, fir_filter.cpp
, fir_filter_mod.cpp
, fir_filter.h
, fir_filter_mod.h
和testbench.cpp
。 基本上,我使用testbench來檢查fir_filter.cpp
和fir_filter_mod.cpp
之間的結果。
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
#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
void fir_filter(double *x, double *y);
#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;
}
#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;
}
double fir_filter_mod(double *x);
#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;
}
}
}
壞主意:在程序中的屏幕代碼上打印很多內容。 然后,您可以檢查程序中的最后一條消息,並嘗試查找發生段錯誤的位置。 您還可以打印調試數據,例如不同變量的值。
好主意:使用調試器。 這使得不必要的壞主意實現。
另一個好主意:嘗試對代碼進行審核。 有很多C ++代碼的分析器。 它們可以揭示程序中潛在的危險位置。
錯誤在這里:
if(*y == y_mod) {
cout << "ERROR" << endl;
在上一步中,您調用了void fir_filter(double *x, double *y)
,它不會更改指針值。 但是您不要在程序開始時初始化y。 所以y
指向無處,而y
取消引用使程序崩潰。
這部分有一個問題
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);
因為對fir_filter
的調用會將結果寫入*y
,但是指針沒有指向任何地方。
fir_filter.cpp的這一行存在分段錯誤
*y = temp;
因為y指向無處。
如果要獲取該值,只需將其返回或使用引用即可。
您需要的是調試器。 嘗試在gdb中運行程序,查看崩潰發生的位置:
`gdb my_tb`
輸入run
命令以啟動程序運行。 發生崩潰時,可以使用bt
命令查看stacktrace。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.