[PLUG] C++ pointer freeing problem
Wayne E. Van Loon Sr.
wevl at pacifier.com
Sun Feb 14 20:10:09 UTC 2010
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
> PLUG at lists.pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug
>
>
>
More information about the PLUG
mailing list