I am reading some code sample that sounds odd to me:
Method argument is a pointer to double. In method body, there a conditional statement testing whether this argument pointer is given or not (empty or not). I wonder how the pointer can have a bool value, and what is the syntax for non-existant pointer (NULL? 0?).
Here is the method implementation :
bool OMethod::initSim(double* alpha_)
{
int i, j;
const double * cparameters = fun->getParameters();
simplex = OMatrix::allocMatrix(npts,ndim,true);
sSum = new double[ndim];
funcEvals = new double[npts];
double *alpha = 0;
if(!alpha_) {
alpha = new double[ndim];
for(i = 0; i< ndim; ++i) {
if(cparameters[i] == 0)
alpha[i] = 0.2f;
else
alpha[i] = 0.1 * cparameters[i];
}
}
else
{
alpha = alpha_;
}
for(j = 0; j < npts; ++j)
{
for(i = 0; i < ndim; ++i)
sim[j][i] = cparameters[i]
}
delete [] cparameters;
for(j = 0; j < ndim; ++j)
{
sim[j + 1][j] += alpha[j];
}
computeS();
for(i = 0; i < npts; ++i)
{
funcEvals[i] = evalFunc(simplex[i]);
}
In C++, a pointer value is considered “true” in a boolean context (e.g. in an
iftest or!expression) when it is not equal to zero, i.e. it is not null. So,!alphais the same asalpha == 0,alpha == NULLandalpha == nullptr.