Carlos Konstanski wrote:
> 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);
> }
>   
You are extracting individual characters out of rawInput by getting a 
pointer to a character array, then getting characters 'the C way'.

Yet the string class has a character extraction operator -- why not just 
use rawInput[i], or rawInput.at(i)?

-- 
Tim Wescott
Wescott Design Services
Voice: 503-631-7815
Cell:  503-349-8432
http://www.wescottdesign.com


_______________________________________________
PLUG mailing list
PLUG@lists.pdxlinux.org
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to