But from the declaration up above, rawInput is a string. You don't
delete C++ style strings, do you? Isn't that an automatic feature? Is
it also named rawInput outside of this function? (Not that that's a
problem, just wondering where you're accessing it and how when it's
failing.)

charSequence should not be messed with as far as trying to delete it.
That gets taken care of automatically by the compiler.
http://www.cplusplus.com/forum/general/15529/

What is the error? I'm not an expert, but I've had to take a few
classes and deal with Visual C++ and it's sometimes bizarre error
reporting.

Also I wouldn't mind running your code through the compiler and taking
a look. But like I said, I'm just a student myself, just a little
ahead of you in C++. Of course I would need the rest of it, too.

Erik

On Sun, Feb 14, 2010 at 12:10 PM, Wayne E. Van Loon Sr.
<[email protected]> wrote:

> 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.
> Carlos:
> As I see it, charSequence is an automatic variable in bool
> numericP(string rawInput) and the space for that variable does not need
> to be freed.
>
> If you attempt to free what charSequence points to after
>
> charSequence = rawInput.c_str();
>
> then you are attempting to free memory starting at the address that
> rawInput.c_str() returned. I know nothing of rawInput, but I suspect you
> don't want to free that memory here. I suspect that wherever the
> rawInput structure / class was "newed" is where that should happen and
> the rawInput structure / class should free the memory holding the
> string. Is it possible that rawInput is some sort of utility that
> handles deleting / newing when rawInput is updated?
>
> Wayne
>> 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);
>> }
>> _______________________________________________
>> PLUG mailing list
>> [email protected]
>> http://lists.pdxlinux.org/mailman/listinfo/plug
>>
>>
>>
>
> _______________________________________________
> PLUG mailing list
> [email protected]
> http://lists.pdxlinux.org/mailman/listinfo/plug
>
_______________________________________________
PLUG mailing list
[email protected]
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to