Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation fault in a simple C++ program
    text
    copied!<p>The program gives me a <a href="http://en.wikipedia.org/wiki/Segmentation_fault" rel="nofollow">segmentation fault</a>. How can I fix this problem?</p> <p>I have a <code>makefile</code>, <code>fir_filter.cpp</code>, <code>fir_filter_mod.cpp</code>, <code>fir_filter.h</code>, <code>fir_filter_mod.h</code> and <code>testbench.cpp</code>. Basically I use testbench to check the result between <code>fir_filter.cpp</code> and <code>fir_filter_mod.cpp</code>.</p> <h3>File Makefile:</h3> <pre><code>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 </code></pre> <h3>File shift_class.h</h3> <pre><code>#ifndef __SHIFT_CLASS__ #define __SHIFT_CLASS__ template&lt;typename dataType, int NUM_REGS&gt; 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 &lt;&lt; (dataType din){ SHIFT: for(int i = NUM_REGS - 1; i &gt;= 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 </code></pre> <h3>File fir_filter.h</h3> <pre><code>void fir_filter(double *x, double *y); </code></pre> <h3>File fir_filter.cpp</h3> <pre><code>#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&lt;double, 4&gt; regs; double temp = 0; regs &lt;&lt; *x; MAX: for(int i = 0; i &lt; 4; i++) { temp += h[i] * regs[i]; } *y = temp; } </code></pre> <h3>File fir_filter_mod.cpp</h3> <pre><code>#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&lt;double, 4&gt; regs; double y = 0; double temp = 0; regs &lt;&lt; *x; MAX: for(int i = 0; i &lt; 4; i++) { temp += h[i] * regs[i]; } y = temp; return y; } </code></pre> <h3>File fir_filter.h</h3> <pre><code>double fir_filter_mod(double *x); </code></pre> <h3>File testbench.cpp</h3> <pre><code>#include &lt;iostream&gt; #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 = &amp;init; for(int j = 0; j &lt; 5; j++) { fir_filter(x, y); y_mod = fir_filter_mod(x); if(*y == y_mod) { cout &lt;&lt; "ERROR" &lt;&lt; endl; } else { cout &lt;&lt; y_mod &lt;&lt; endl; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload