I encountered a problem in c++. I read some codes,but there was a very wired usage of pointer. The code is following:
double* marginalProbability =
new double [10 * sizeof(marginalProbability[0])];
memset( marginalProbability, 0, 10 * sizeof(double) );
//................
//.................
if(marginalProbability>0)
printf("larger");
else
printf("smaller");
The question I’m asking is what does it mean that if(marginalProbability>0). It is a pointer greater than zero. I think that in a normal compiler, there are no addresses which will be equal to zero. Or are there any other meanings of that? Otherwise, this line seems meaningless. Thanks.
a pointer with the value of zero is a null pointer. A pointer to nothing. Checking that it is not zero (p!=0 is more common that p>0 although p!=nullptr is most correct) means that it is not null.
The code you have listed is also wrong. if the new operation fails it will not return a null pointer but throw an exceptions which will no be caught.
this is what it should be.