Program received signal SIGSEGV, Segmentation fault

Pages: 123
@MikeyBoy: the call stack is empty. This is what I get from the Debugger tab:

Active debugger config: GDB/CDB debugger:Default
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: E:\BC\Courses\Pablo_Methods\HW\Mendoza 2010 Replication\Lia Replication\
Adding source dir: E:\BC\Courses\Pablo_Methods\HW\Mendoza 2010 Replication\Lia Replication\
Adding file: E:\BC\Courses\Pablo_Methods\HW\Mendoza 2010 Replication\Lia Replication\bin\Debug\Lia_Replication.exe
Changing directory to: "E:/BC/Courses/Pablo_Methods/HW/Mendoza 2010 Replication/Lia Replication/."
Set variable: PATH=.;D:\Program Files (x86)\CodeBlocks\MinGW\bin;D:\Program Files (x86)\CodeBlocks\MinGW;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\ia32\compiler;C:\Program Files (x86)\Intel\iCLS Client;C:\Program Files\Intel\iCLS Client;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;D:\Program Files\MATLAB\R2016b\runtime\win64;D:\Program Files\MATLAB\R2016b\bin;D:\Program Files\MATLAB\R2015a\runtime\win64;D:\Program Files\MATLAB\R2015a\bin;D:\Program Files (x86)\QuickTime\QTSystem;C:\Program Files (x86)\Windows Live\Shared;D:\Program Files (x86)\Calibre2;D:\Program Files (x86)\MiKTeX 2.9\miktex\bin;D:\Program Files (x86)\LyX 2.2\Perl\bin;D:\Program Files (x86)\Skype\Phone;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit;C:\ProgramData\chocolatey\bin;D:\MinGW\bin
Starting debugger: D:\Program Files (x86)\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args "E:/BC/Courses/Pablo_Methods/HW/Mendoza 2010 Replication/Lia Replication/bin/Debug/Lia_Replication.exe"
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.6.1
Child process PID: 12780
Program received signal SIGSEGV, Segmentation fault.

Some problems:
line 183 if(i=j) // maybe you mean i == j
line 409 if(templb>=0 & tempvb>=0) maybe you mean &&
line 524 if(jjk+jjb>=1|skip==0) maybe you mean ||
line547 else if(jjk+jjb==0&skip==1) maybe you mean &&

In VS 2015 I get a stack overflow exception from chkstk.asm in the first line of main. I guess too many local arrays. Incresing the stack size to 10 MB fixed it.
@Thomas1965: I corrected all the errors above and did settings -> compiler -> linker settings -> other linker options -> -WI,--stack,10000000

Still I got the same segmentation fault.
Last edited on
@jlb: I will try to add output lines. Thank you for the suggestions!
upload your current code to github or similar.
I changed it under Project->Build options and did a complete rebuild under Build->Rebuild or Crtl-F11
It works now.
https://pasteboard.co/1V0maKjMg.jpg
Last edited on
Instead of playing with increasing the stack space you really really should break the program into quite a few smaller functions instead. And as you're breaking the program into functions try to simplify the logic.


Last edited on
@ne555: I have heard a lot about GitHub. Now I am creating a profile on that website. Thank you for the suggestion!
@Thomas1965: omg it totally did! Thanks so much!
@jlb: I will be fiddling with C++ this summer and this is first thing on the list <\determined>
I put the code onto GitHub. https://github.com/flowerysleeves/replication/blob/master/main.cpp I am confused as to why the code would not stop running when the variable "con" hits 1 in the while loop.
Last edited on
$ g++ main.cpp -ggdb -Wall -Wextra -Wpedantic -fsanitize=undefined
main.cpp:378:33: warning: ‘binding’ may be used uninitialized in this function [-Wmaybe-uninitialized]

`binding' is a boolean, you never set it to anything.

(in bold is the output of the sanitizer)
$ ./a.out
omega=1.8461
sigma=2
delta=0.0880044
beta=0.305367
a=2.75
main.cpp:297:21: runtime error: index 8 out of bounds for type 'double [8]' (sa)
sa[m]=exp(Ashock[l])*abar; // state for TFP
main.cpp:298:21: runtime error: index 8 out of bounds for type 'double [8]' (sr)
sr[m]=exp(Rshock[i])*(1+rbar); // state for the interest rate
main.cpp:299:29: runtime error: index 8 out of bounds for type 'double [8]' (qb)
qb[m]=1/sr[m]; // state for bond prices
main.cpp:299:21: runtime error: index 8 out of bounds for type 'double [8]' (sr)
qb[m]=1/sr[m]; // state for bond prices
main.cpp:300:21: runtime error: index 8 out of bounds for type 'double [8]' (sp)
sp[m]=exp(Pshock[j])*pbar; // state for price of int'l goods
main.cpp:343:28: runtime error: index 8 out of bounds for type 'int [8]'
kzt[i][j][l]=-999999999;
main.cpp:344:28: runtime error: index 8 out of bounds for type 'int [8]'
kz2[i][j][l]=-999999999;
con5=0
con=1

Now look at the loops where those invalid access happen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    m=0;
    double sa[8];
    double sr[8];
    double qb[8];
    double sp[8];
    for(i=0;i<nr;i=i+1){ //¿why aren't i, j, l, local to the loops?
        for(j=0;j<np;j=j+1){
            for(l=0;l<na;l=l+1){
                m=m+1; //<-- m starts at 1
                sa[m]=exp(Ashock[l])*abar;
                sr[m]=exp(Rshock[i])*(1+rbar);
                qb[m]=1/sr[m];
                sp[m]=exp(Pshock[j])*pbar;
            }
        }
    }
also, it is not inmediate obvious that the size of the arrays is na*np*nr

1
2
3
4
5
6
7
8
    for(i=0;i<ne;i=i+1){
        for(j=0;j<nb;j=j+1){
            for(l=0;l<nk;l=l+1){
                kzt[i][j][l]=-999999999;
                kz2[i][j][l]=-999999999;
            }
        }
    } // initialize the values of kzt and kz2 to make sure that we don't stop at the first iteration 

if you see the declaration
1
2
int kzt[nk][nb][ne]; // counter for last period's capital
int kz2[nk][nb][ne]; // counter for capital from two periods ago 
you've got i and l backwards
@ne555: I updated the code. Binding is now set to 1 under some coditions. errors with sa etc are fixed, and the i and l are switched. Now I need to fix some numerical problems. Some values are supposed to be positive but they are always negative. Thank you for looking it over!
If it is okay, I would like to ask another question: with ofstream, I am able to output a lot of variables to .csv files except for a variable called "con".
1
2
    ofstream record_con;
    record_con.open("con.csv");


and then later:record_con << con << endl;

all of the code is on github in a previous reply.


> Binding is now set to 1 under some coditions
binding appears two times in all your code
1
2
bool binding; //line 90, declaration
if(binding==1){ //line 400, use 
you never initialise it.
¿what branch do you think your code will follow?


> Now I need to fix some numerical problems. Some values are supposed to be
> positive but they are always negative.
I still got some invalid access. Now for the `V' matrix, lines 468 and 475.
use the -fsanitize=undefined when compiling and it will pinpoint them for you when you run your program.

Your code is hard to follow: meaningless variable names, recycled iterators, lack of functions, lots of code...
Also, I know nothing about the problem you are trying to solve or the method that you're following.
Sorry, but you are on your own there.


> I am able to output a lot of variables to .csv files except for a variable
> called "con".
you need to describe the issue better.
when I run your code, it generates a `con.csv' file with twenty-six lines with a 0, and then some lines with 26 (I had killed the program)
¿you don't have the `con.csv' file? ¿is it empty?
@ne555:

> Binding problem: now I realized that the variable was not useful, so I took it away

> using the Undefined Behavior Sanitizer is a little beyond me at the moment as I am using an IDE on Windows that has the compiler linked to it, but I am excited about it. A couple other people have mentioned that I needed to have functions, so I need to learn that as well.

> I don't have the file. All the other ones are there except for this one lol But it's okay. I have cout to go to in this case.

Thanks again for your help!
Last edited on
@ne555: while attempting to create a function I have encountered the following error while compiling:


E:\BC\Courses\Pablo_Methods\HW\Mendoza 2010 Replication\Lia Replication\main.cpp|142|error: cannot convert 'double (*)[8][8]' to 'double***' in return|


Do you have any idea what is wrong, please? Thank you very much!
Last edited on
don't return arrays from functions. Those variables are local and will be destroyed when the function ends.
Instead pass it as an extra argument to the function or use std::array

1
2
3
4
void joint_stationary_distribution_k_v_e(double V[nk][nb][ne],double pi[ne][ne],double kkn[nk][nb][ne], double bbn[nk][nb][ne], double pka[nk][nb][ne]);
//double*** pkve=joint_stationary_distribution_k_v_e(V,pi,kkn,bbn);
double pkve[nk][nb][ne];
joint_stationary_distribution_k_v_e(V,pi,kkn,bbn,pkve);



std::array has some advantages, like knowing its own size and being copyable.
Also, take a look at std::valarray that works as a mathematical vector
@ne555: I made changes but I wasn't able to get any output from pkve in the main program. :-/
Last edited on
> using the Undefined Behavior Sanitizer is a little beyond me at the moment as
> I am using an IDE on Windows that has the compiler linked to it
you may use http://coliru.stacked-crooked.com/ to compile and run your code

integer overflow on line 682
out of bounds in 659 and 666 (`V' matrix)


> I wasn't able to get any output from pkve in the main program. :-/
I've got -nan (not a number)
Check out your computations, pka2 keeps growing.
Pages: 123