[PLUG] C++ pointer freeing problem
Carlos Konstanski
ckonstanski at pippiandcarlos.com
Sun Feb 14 18:46:23 UTC 2010
I have a C++ function I'm writing; see below. I'm new to C++, and the
implementation details tend to bite me where the sun don't shine
whenever they can.
The function works as intended. But later attempts to use the value
that I passed in as rawInput cause an error in Visual C++. GNU C++
does not give me any error. Nevertheless, I think that there must be a
problem here. I suspect that the pointer charSequence must need to be
freed. But I cannot find any way to do this that compiles.
Is this my problem? If so, how do I free charSequence?
Carlos
bool numericP(string rawInput) {
bool legalCharsP = true;
int numDecimalPoints = 0;
int i = 0;
const char *charSequence = rawInput.c_str();
while(i < rawInput.size() && legalCharsP && numDecimalPoints <= 1) {
// the first char can be a -, ., or a digit; all others must
// be a . or a digit; but there can be only one or less .'s
if(! ((i == 0 && charSequence[i] == 0x2d) ||
((charSequence[i] >= 0x30 && charSequence[i] <= 0x39) || charSequence[i] == 0x2e))) {
legalCharsP = false;
} else if(charSequence[i] == 0x2e) {
numDecimalPoints++;
}
i++;
}
return (legalCharsP && numDecimalPoints <= 1);
}
More information about the PLUG
mailing list