Matrix not getting initialized
-
Consider the following code "main.cpp"
#include
#include "../include/ESSolver.h"int mu;
int lambda ;int main(void)
{
mu = 5;
lambda = 10;ESSolver es; es.Init(); es.Optimize(); return 0;
}
"ESSolver.h"
extern int mu;
extern int lambda ;class ESSolver
{
public:
const static int nVar = 4;ESSolver(); ~ESSolver(void); void Init(); void Optimize(); int rand\_r(int, int);
private:
double (*muPop)[nVar];
double *muSigma;double (\*lambdaPop)\[nVar\]; double \*lambdaSigma; double (\*mulambdaPop)\[nVar\]; double \*mulambdaSigma;
};
"ESSolver.cpp"
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>#include "../include/ESSolver.h"
using namespace std;
ESSolver::ESSolver()
{
double _muPop [mu][nVar];
muPop = _muPop;
muSigma = new double[mu];double \_lambdaPop \[lambda\]\[nVar\]; lambdaPop = \_lambdaPop; lambdaSigma = new double\[lambda\]; double \_mulambdaPop \[mu+lambda\]\[nVar\]; mulambdaPop = \_mulambdaPop; mulambdaSigma = new double\[mu+lambda\]; return;
}
ESSolver::~ESSolver(void)
{
if (muSigma) delete muSigma;
if (lambdaSigma) delete lambdaSigma;
if (mulambdaSigma) delete mulambdaSigma;
return;
}void ESSolver::Init()
{
// Initializing object variable
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
muPop[i][j] = 50;
}
}cout << "\\n----- muPop -----\\n"; for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { cout << muPop\[i\]\[j\] << " "; } cout << endl; }
}
void ESSolver::Optimize()
{
cout << "\n----- muPop -----\n";
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
cout << muPop[i][j] << " ";
}
cout << endl;
}
}On running the following code I am getting Output [^] As you can see from the image, Matrix is not getting initialized. Can anybody tell me why.
-
Consider the following code "main.cpp"
#include
#include "../include/ESSolver.h"int mu;
int lambda ;int main(void)
{
mu = 5;
lambda = 10;ESSolver es; es.Init(); es.Optimize(); return 0;
}
"ESSolver.h"
extern int mu;
extern int lambda ;class ESSolver
{
public:
const static int nVar = 4;ESSolver(); ~ESSolver(void); void Init(); void Optimize(); int rand\_r(int, int);
private:
double (*muPop)[nVar];
double *muSigma;double (\*lambdaPop)\[nVar\]; double \*lambdaSigma; double (\*mulambdaPop)\[nVar\]; double \*mulambdaSigma;
};
"ESSolver.cpp"
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>#include "../include/ESSolver.h"
using namespace std;
ESSolver::ESSolver()
{
double _muPop [mu][nVar];
muPop = _muPop;
muSigma = new double[mu];double \_lambdaPop \[lambda\]\[nVar\]; lambdaPop = \_lambdaPop; lambdaSigma = new double\[lambda\]; double \_mulambdaPop \[mu+lambda\]\[nVar\]; mulambdaPop = \_mulambdaPop; mulambdaSigma = new double\[mu+lambda\]; return;
}
ESSolver::~ESSolver(void)
{
if (muSigma) delete muSigma;
if (lambdaSigma) delete lambdaSigma;
if (mulambdaSigma) delete mulambdaSigma;
return;
}void ESSolver::Init()
{
// Initializing object variable
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
muPop[i][j] = 50;
}
}cout << "\\n----- muPop -----\\n"; for (int i = 0; i < mu; i++) { for (int j = 0; j < nVar; j++) { cout << muPop\[i\]\[j\] << " "; } cout << endl; }
}
void ESSolver::Optimize()
{
cout << "\n----- muPop -----\n";
for (int i = 0; i < mu; i++)
{
for (int j = 0; j < nVar; j++)
{
cout << muPop[i][j] << " ";
}
cout << endl;
}
}On running the following code I am getting Output [^] As you can see from the image, Matrix is not getting initialized. Can anybody tell me why.
-
Quote:
double _muPop [mu][nVar]; muPop = _muPop;
_mPop
is a temporary array (a local variable), yourmuPop
pointer is goingo to point to garbage.