Problem with Running and Debugging

Mar 23, 2017 at 12:57am
Hi, I am trying to implement some simple function for a class project in Codeblocks. The code compiled fine but will not run. When I ran the debugger I keep getting this error message, but when I searched online I couldn't find information on this error. Could it be because the MinGW is old and I need to update it? Thank you.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
Starting debugger: D:\Program Files (x86)\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet  -args E:/BC/Courses/Pablo_Methods/HW/HW1/Tauchen/bin/Debug/Tauchen.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: 6160
In __cxa_throw () ()
#5  0x0042f718 in __gnu_cxx::__alloc_traits<std::allocator<double> >::allocate (__a=..., __n=4313280) at D:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/ext/alloc_traits.h:182


D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\ext\alloc_traits.h:182:6209:beg:0x42f718
At D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\ext\alloc_traits.h:182
Mar 23, 2017 at 2:18am
Posting the code of your program would be a good idea.
Mar 23, 2017 at 9:06pm
Here is the code, the first bits for calculating the cumulative distribution function is obtained from online:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// Returns the erf() of a value (not super precice, but ok)
double erf(double x)
{
 double y = 1.0 / ( 1.0 + 0.3275911 * x);
 return 1 - (((((
        + 1.061405429  * y
        - 1.453152027) * y
        + 1.421413741) * y
        - 0.284496736) * y
        + 0.254829592) * y)
        * exp (-x * x);
}

// Returns the probability of [-inf,x] of a gaussian distribution
double cdf(double x, double mu, double sigma)
{
	return 0.5 * (1 + erf((x - mu) / (sigma * sqrt(2.))));
}

void tauchen(double rho, double mu, int sigma, int n, int kappa)
{
	double zmin = mu - kappa*sigma / pow((1 - pow(rho,2)),0.5);
	double zmax = mu + kappa*sigma / pow((1 - pow(rho,2)),0.5);
	double zdel = (zmax - zmin) / (n - 1);
	vector<double> z(n);

	for (int i = 1; i < n+1; i=i+1)
		z[i] = zmin + (i-1)*zdel;

    vector<double> m(n-1);

    for (int i = 1; i<n; i=i+1)
        m[i] = (z[i]+z[i+1])/2;

    vector<double> vecmu(n,mu);
    vector<double> a(n);
    for (int i = 1; i<n+1; i=i+1)
        a[i] = (1-rho)*vecmu[i] + rho*z[i];

    vector<vector<double> > ztrans(n,vector<double>(n));
    for (int j=1;j<n+1;j=j+1)
    {
        ztrans[j][1] = cdf(m[1],a[j],pow(sigma,2));

        for (int i=2;i<n;i=i+1)
            ztrans[j][i] = cdf(m[i],a[j],pow(sigma,2)) - cdf(m[i-1],a[j],pow(sigma,2));

        ztrans[j][n] = 1-cdf(m[n],a[j],pow(sigma,2));
    }
}

int main()
{
    double rho,mu;
    int sigma,n,kappa;
    vector<double> z(n);
    vector<vector<double> > ztrans(n,vector<double>(n));
  	cout << "please enter the variables rho, mu, sigma, n, and kappa:";
    cin >> rho >> mu >> sigma >> n >> kappa;
	tauchen(rho,mu,sigma,n,kappa);
    for (int i=1;i<n+1;i=i+1)
        cout << z[i];

    for (int i=1;i<n+1;i=i+1)
    {
        for (int j=1;j<n+1;j=j+1)
        cout << ztrans[j][i];
    }
	return 0;
}
Mar 23, 2017 at 9:26pm
Amongst other things:
- You are trying to create dynamic arrays z and ztrans on lines 61 and 62 using n - but n isn't input until line 64; re-order this.

- You appear to be creating z twice - on lines 61 and 30; in main() it won't know anything about the other version; the same appears to apply to ztrans. Should z and ztrans be declared in main() and then sent as arguments to tauchen()?

- Arrays start from 0 in c++, so anything declared with n elements is going to be indexed from 0 to n-1; you are probably going to go beyond array bounds on lines 66/67; 69/71/72; 32/33; 37/38; 42/43; 46/48/51/53 - possibly many more.

- There is no point in using pow() for small powers like 2; just use x * x. Similarly, prefer sqrt() to using pow() with exponent 0.5.

- Is there any good reason why you are calling cdf() with the variance,sigma squared, when it's defined in terms of standard deviation sigma?

- Why do you need the array vecmu[]? It appears to be a constant (equal to the mean).


Last edited on Mar 23, 2017 at 10:01pm
Mar 24, 2017 at 2:21am
1
2
3
4
5
6
7
8
9
10
int main()
{
    double rho,mu;
    int sigma,n,kappa; // **** n is not initialised
    vector<double> z(n); // **** this is iundefined behaviour;
                         // you were lucky that this bombed with either bad_alloc or bad_array_new_length
                         // In __cxa_throw () ... in __gnu_cxx::__alloc_traits<std::allocator<double> >::allocate (__a=..., __n=4313280)
                         // a more intelligent debugger (eg. the Visual Studio debugger) would have pin-pointed the exception that was thrown
    // ...
}
Mar 25, 2017 at 6:45pm
I changed the code according to @lastchance's suggestions except for vecmu[]. vecmu[] was built in order to change a constant into a vector so that in the equation below the two terms are of the same dimensions. @JLBorges, when you said that n was not initialized, did you mean that I have to write int sigma; int n; int kappa? As a followup question, I tried to install Visual Studio Community 2017 to have a graphical interface but I didn't know which extra components to install in order to be able to run the compiler and the debugger from the interface. Do you have any suggestions, please?

At the moment my code looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// Returns the erf() of a value (not super precice, but ok)
double erf(double x)
{
 double y = 1.0 / ( 1.0 + 0.3275911 * x);
 return 1 - (((((
        + 1.061405429  * y
        - 1.453152027) * y
        + 1.421413741) * y
        - 0.284496736) * y
        + 0.254829592) * y)
        * exp (-x * x);
}

// Returns the probability of [-inf,x] of a gaussian distribution
double cdf(double x, double mu, double sigma)
{
	return 0.5 * (1 + erf((x - mu) / (sigma * sqrt(2.))));
}

void tauchen(double rho,double mu,int sigma,int n,int kappa,vector<double>z,vector<vector<double> > ztrans)
{
	double zmin = mu - kappa*sigma / sqrt((1 - rho*rho));
	double zmax = mu + kappa*sigma / sqrt((1 - rho*rho));
	double zdel = (zmax - zmin) / (n - 1);

	for (int i = 0; i < n; i++)
		z[i] = zmin + (i-1)*zdel;

    vector<double> m(n-1);

    for (int i = 0; i<n-1; i++)
        m[i] = (z[i]+z[i+1])/2;

    vector<double> vecmu(n,mu);
    vector<double> a(n);
    for (int i = 0; i<n; i++)
        a[i] = (1-rho)*vecmu[i] + rho*z[i];

    for (int j=0;j<n;j++)
    {
        ztrans[j][0] = cdf(m[0],a[j],sigma);

        for (int i=1;i<n;i++)
            ztrans[j][i] = cdf(m[i],a[j],sigma) - cdf(m[i-1],a[j],sigma);

        ztrans[j][n] = 1-cdf(m[n],a[j],sigma);
    }
}

int main()
{
    double rho,mu;
    int sigma,n,kappa;
  	cout << "please enter the variables rho, mu, sigma, n, and kappa:";
    cin >> rho >> mu >> sigma >> n >> kappa;
    vector<double> z(n);
    vector<vector<double> > ztrans(n,vector<double>(n));
	tauchen(rho,mu,sigma,n,kappa,z,ztrans);
    for (int i=0;i<n;i++)
        cout << z[i];

    for (int i=0;i<n;i++)
    {
        for (int j=0;j<n;j++)
        cout << ztrans[j][i];
    }
	return 0;
}


and I am able to get a chance to enter the values. However, after I enter the values, I get the following error messages:

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: 7300
In __cxa_throw () ()
#3  0x0042f4f0 in __gnu_cxx::__alloc_traits<std::allocator<double> >::allocate (__a=..., __n=4294967295) at D:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/ext/alloc_traits.h:182


D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\ext\alloc_traits.h:182:6209:beg:0x42f4f0
At D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\ext\alloc_traits.h:182
Mar 25, 2017 at 7:10pm
(1) In tauchen, z and ztrans need to be references if their values are to be passed back to main; put an & between their types and their names in the declaration of tauchen (but NOT in the call from main()!)

(2) sigma should be a double (in main, and in the declaration of tauchen)
- presumably it means standard deviation

(3) The declaration of m should be
vector<double> m(n);
NOT
vector<double> m(n-1);
(This is probably the major source of the crash.)
Also, m[] is only set as far as m[n-2] - however, you will use m[n-1] on line 49

(4) I don't know what kappa is (can you explain?) - check whether this should be int or double

(5) vecmu is NOT needed - there is absolutely nothing wrong with writing
a[i] = (1-rho)*mu + rho*z[i];

(6) Line 51: both ztrans and m are going to go beyond array bounds: anything could happen! The last element for a size n array will be [n-1]

I recognise some, but not all, of what you are doing. However, if you need further help it may aid us if you gave some description of what is being computed.
Last edited on Mar 25, 2017 at 7:16pm
Mar 26, 2017 at 1:28am
> @JLBorges, when you said that n was not initialized, did you mean that I have to write
> int sigma; int n; int kappa?

No, I meant that you have to give a value to n before trying to use the value that it holds.

this is an error:
1
2
3
int n ;
// n is uninitialised at this point
vector<double> z(n); // attempt to use uninitialised value of n 


This is fine:
1
2
int n = 5000 ;
vector<double> z(n);


This too is fine:
1
2
3
int n ;
std:cin >> n ; // enter 5000
vector<double> z(n);



> install Visual Studio Community 2017

Download the installer from https://www.visualstudio.com/vs/community/?rr=https%3A%2F%2Fduckduckgo.com%2F

In the installer, choose 'Desktop development with C++'

In 'Desktop development with C++'

a. check 'VC++ 2017 v141 toolset (x86,x64)'
b. check 'Clang/C2 (experimental)

Uncheck everything else, for now.
If required, additional components can be installed later.
Mar 27, 2017 at 4:41pm
I made adjustments based on the suggestions. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// Returns the erf() of a value (not super precice, but ok)
double erf(double x)
{
 double y = 1.0 / ( 1.0 + 0.3275911 * x);
 return 1 - (((((
        + 1.061405429  * y
        - 1.453152027) * y
        + 1.421413741) * y
        - 0.284496736) * y
        + 0.254829592) * y)
        * exp (-x * x);
}

// Returns the probability of [-inf,x] of a gaussian distribution
double cdf(double x, double mu, double sigma)
{
	return 0.5 * (1 + erf((x - mu) / (sigma * sqrt(2.))));
}

void tauchen(double rho,double mu,double sigma,int n,int kappa,vector<double> & z,vector<vector<double> > & ztrans)
{
	double zmin = mu - kappa*sigma / sqrt((1 - rho*rho));
	double zmax = mu + kappa*sigma / sqrt((1 - rho*rho));
	double zdel = (zmax - zmin) / (n - 1);

	for (int i = 0; i < n; i++)
		z[i] = zmin + (i-1)*zdel;

    vector<double> m(n-1);

    for (int i = 0; i<n-1; i++)
        m[i] = (z[i]+z[i+1])/2; // there are n-1 elements in this vector because z has n elements and m is the mean between two neighboring elements in z

    vector<double> a(n);
    for (int i = 0; i<n; i++)
        a[i] = (1-rho)*mu + rho*z[i];

    for (int j=0;j<n;j++)
    {
        ztrans[j][0] = cdf(m[0],a[j],sigma);

        for (int i=1;i<n-1;i++)
            ztrans[j][i] = cdf(m[i],a[j],sigma) - cdf(m[i-1],a[j],sigma);

        ztrans[j][n-1] = 1-cdf(m[n-2],a[j],sigma);
    }
}

int main()
{
    double rho,mu,sigma;
    int n,kappa;
  	cout << "please enter the variables rho, mu, sigma, n, and kappa:";
    cin >> rho >> mu >> sigma >> n >> kappa;
    vector<double> z(n);
    vector<vector<double> > ztrans(n,vector<double>(n));
	tauchen(rho,mu,sigma,n,kappa,z,ztrans);
    for (int i=0;i<n;i++)
        cout << z[i];

    for (int i=0;i<n;i++)
    {
        for (int j=0;j<n;j++)
        cout << ztrans[j][i];
    }
	return 0;
}


What I am trying to do is to discretize an AR(1) process. The algorithm is to pick n points between (mu - kappa*sigma/sqrt(1-rho^2)) and (mu + kappa*sigma/sqrt(1-rho^2)), then compute the midpoints m_{i,i+1} = (z_i + z_{i+1})/2 , then determine the transition probabilities for z_i,z_j as F(z_i|z_j) = PHI(m_{1,2};a_j,sigma^2) if i ==1; F(z_i|z_j) = PHI(m_{i,i+1};a_j,sigma^2) - PHI(m_{i-1,i};a_j,sigma^2) if i ==2,...,n-1; F(z_i|z_j) = 1 - PHI(m_{n-1,n};a_j,sigma^2) if i ==n. a_j = (1-rho)*mu + rho*z_j. PHI is the normal cdf.

The error message I get is
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: 12252
In __cxa_throw () ()
#5  0x0042f308 in __gnu_cxx::__alloc_traits<std::allocator<double> >::allocate (__a=..., __n=4199040) at D:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/4.9.2/include/c++/ext/alloc_traits.h:182


D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\ext\alloc_traits.h:182:6209:beg:0x42f308
At D:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\ext\alloc_traits.h:182


I also reinstalled Visual Studio but when I clicked "build" there were 462 errors, the first of which was "cannot open source file "errno.h"". This is the same problem I encountered when I installed this for the first time.
Mar 27, 2017 at 4:57pm
I ran the code you have posted. Apart from scrunching up all the output (add some linefeeds, <<endl, on lines 64 and 69), it ran fine with both g++ and cl.exe compilers from the command line.

Guessing somewhat at the data I used
rho = 0.5
mu = 1.0
sigma = 0.5
n = 10
kappa = 3
Mar 27, 2017 at 5:33pm
@lastchance I tried to use the g++ compiler from the command line using some instructions from the internet. After getting the a.exe file, I clicked on it, and entered the values. Then I got this message "terminate called after throwing an instance of std bad_alloc"
Mar 27, 2017 at 6:35pm
It is running on all the compilers I try (including CPP shell).

Just a couple of things to try:
- sorry to ask, but is the code you have posted the same as the file you are compiling and executing? (copy and paste it back into a new, empty folder);

- if I turn error messages up to maximum in C++ shell it does point out that there is a standard function erf() in <cmath>, part of the standard since C++11 and possibly in compilers before that; try renaming all occurrences of erf() (to my_erf(), say) to see if that was the cause of conflict.

Otherwise, it seems to run fine with the compilers I have tried - just scrunches up the output because there are no line feeds.


BTW, if you compiled from the command prompt, why are you "clicking" on a.exe to run it? Just enter a.exe (or even just... a ) and it should run.


Also, try NOT setting any debug options, and NOT running it through a debugger.
Last edited on Mar 27, 2017 at 6:38pm
Mar 27, 2017 at 7:22pm
@lastchance I pasted the above into a new file Tauchen2.cpp and changed erf to my_erf in the two places where they appeared. The code now looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

// Returns the erf() of a value (not super precice, but ok)
double my_erf(double x)
{
 double y = 1.0 / ( 1.0 + 0.3275911 * x);
 return 1 - (((((
        + 1.061405429  * y
        - 1.453152027) * y
        + 1.421413741) * y
        - 0.284496736) * y
        + 0.254829592) * y)
        * exp (-x * x);
}

// Returns the probability of [-inf,x] of a gaussian distribution
double cdf(double x, double mu, double sigma)
{
	return 0.5 * (1 + my_erf((x - mu) / (sigma * sqrt(2.))));
}

void tauchen(double rho,double mu,double sigma,int n,int kappa,vector<double> & z,vector<vector<double> > & ztrans)
{
	double zmin = mu - kappa*sigma / sqrt((1 - rho*rho));
	double zmax = mu + kappa*sigma / sqrt((1 - rho*rho));
	double zdel = (zmax - zmin) / (n - 1);

	for (int i = 0; i < n; i++)
		z[i] = zmin + (i-1)*zdel;

    vector<double> m(n-1);

    for (int i = 0; i<n-1; i++)
        m[i] = (z[i]+z[i+1])/2; // there are n-1 elements in this vector because z has n elements and m is the mean between two neighboring elements in z

    vector<double> a(n);
    for (int i = 0; i<n; i++)
        a[i] = (1-rho)*mu + rho*z[i];

    for (int j=0;j<n;j++)
    {
        ztrans[j][0] = cdf(m[0],a[j],sigma);

        for (int i=1;i<n-1;i++)
            ztrans[j][i] = cdf(m[i],a[j],sigma) - cdf(m[i-1],a[j],sigma);

        ztrans[j][n-1] = 1-cdf(m[n-2],a[j],sigma);
    }
}

int main()
{
    double rho,mu,sigma;
    int n,kappa;
  	cout << "please enter the variables rho, mu, sigma, n, and kappa:";
    cin >> rho >> mu >> sigma >> n >> kappa;
    vector<double> z(n);
    vector<vector<double> > ztrans(n,vector<double>(n));
	tauchen(rho,mu,sigma,n,kappa,z,ztrans);
    for (int i=0;i<n;i++)
        cout << z[i];

    for (int i=0;i<n;i++)
    {
        for (int j=0;j<n;j++)
        cout << ztrans[j][i];
    }
	return 0;
}

Then I compiled on the command line with the command "g++ Tauchen2.cpp". Then I typed "a". Then I entered the same values as you used. But I got
terminate called after throwing an instance of 'std::bad_alloc' what():
 std::bad_alloc This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
This is with the "libstdc++-6.dll" already pasted into the folder where the .exe file is located. Now I am thinking about deleting the existing MinGW folder I have and reinstalling Code Blocks that comes prepackaged with MinGW.

When I tried the cl compiler this is the error message I got
c:\..\Tools\MSVC\14.10.25017\include\crtdefs.h(10): fatal error C1083: Cannot open include file:'corecrt.h': No such file or directory
Do I need to install some other component of Visual Studio in order to have this .h file?
Last edited on Mar 27, 2017 at 7:29pm
Mar 27, 2017 at 7:59pm
@lifeisgood,
How are you entering your data? The only way that I could replicate your error was if I (incorrectly) put commas between items when entering them.

Use the g++ compiler. When prompted, enter the data as
0.5 1.0 0.5 10 3

i.e. just spaces between items, NOT commas.

When you can finally see some output, please put the appropriate line feeds in.
Last edited on Mar 27, 2017 at 8:00pm
Mar 27, 2017 at 8:08pm
It worked, thank you so much!
Mar 27, 2017 at 10:55pm
@OP

lastchance wrote:
- if I turn error messages up to maximum in C++ shell it does point out that there is a standard function erf() in <cmath>, part of the standard since C++11 and possibly in compilers before that; try renaming all occurrences of erf() (to my_erf(), say) to see if that was the cause of conflict.


To me, that reinforces 2 ideas:

1. Set your warnings levels to a high level;
2. Don't have using namespace std;

With 1:

Warnings are your friend, they tell you about potential problems with the code. Especially for code that compiles, but crashes at runtime - probably because of the things mentioned in the warnings. Using g++ or clang++, use at least:

g++ -std=c++14 -Wall -Wextra pedantic-errors -o ExeFileName *.cpp

I use these ones as well: http://www.cplusplus.com/forum/general/183731/#msg899203

Then I compiled on the command line with the command "g++ Tauchen2.cpp".


You can see now that is horribly minimal :+(


It's worth reading the manual : https://gcc.gnu.org/onlinedocs/

I am not sure, but if using Visual Studio you may have to turn some individual warnings on manually.

With 2:

There are lots of things in the STL that have names which might unwittingly conflict with your variable function names. Some of the obvious ones are std::left, std::right, std::distance, but this topic may uncovered another one. So this is the prime reason for not having using namespace std; In the end the easiest thing to do is to put std:: before each std thing. There are other things that can be done, but this is the easiest in the end. If you look at code by the experts on this site, they always do this.

Beginners can often get away with this, but at some point it will bite them in the ass :+)

Good Luck!!
Topic archived. No new replies allowed.