views:

41

answers:

1

I have a tree structure and I want to find all nodes matching a given criteria. Each time I call the find function, it returns next matching node. Children are searched by recursive function call.

For some reason a key comparison of pointers fails for this implementation. Please see the code below, I have pointed out the failing comparison.

HtmlTag* HtmlContent::FindTag(string tagName, string tagParameterContent)
{
    if (tagName.empty() && tagParameterContent.empty())
        return NULL;

    if (this->startTag == NULL)
        return NULL;

    this->findContinue = this->FindChildren(this->startTag, &tagName, &tagParameterContent);
    return this->findContinue;
}

HtmlTag* HtmlContent::FindChildren(HtmlTag* firstTag, string* tagName, string* tagParameterContent)
{
    HtmlTag* currentTag = firstTag;
    HtmlTag* childrenFound = NULL;

    while (currentTag != NULL)
    {
        if (!tagName->empty() && *tagName == currentTag->tagName)
        {
            if (tagParameterContent->empty() || currentTag->tagParameters.find(*tagParameterContent, 0) != -1)
            {
                if (this->findContinue == NULL)
                    break; // break now when found
                else if (this->findContinue == currentTag) // TODO why this fails?
                    this->findContinue == NULL; // break on next find
            }
        }

        if (currentTag->pFirstChild != NULL)
        {
            childrenFound = this->FindChildren(currentTag->pFirstChild, tagName, tagParameterContent);
            if (childrenFound != NULL)
            {
                currentTag = childrenFound;
                break;
            }
        }

        currentTag = currentTag->pNextSibling;
    }

    return currentTag;
}

VC++ compiler accepts this code but for some reason I can't put a breakpoint on this comparison. I guess this is optimized out, but why? Why this comparison fails?

+4  A: 

I think that you shoud replace == with = in assignment after comparison. Compiler optimalized this whole section because it doesnt do anything useful.

Zuljin
This is how code review works, thanks :)
kaupov