recursive function kills me

Discussion in 'C++' started by Uwe Grawert, Mar 21, 2006.

  1. Uwe Grawert

    Uwe Grawert Guest

    I have the following recursive function:

    string find_value_by_key (xmlDocPtr doc,
    xmlNodePtr root_node,
    const string& key)
    {
    while(root_node != NULL)
    {
    if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
    cout << root_node->name << ": " << xmlNodeListGetString(doc,
    root_node->xmlChildrenNode, 1);
    return string((char*) xmlNodeListGetString(doc,
    root_node->xmlChildrenNode, 1));
    }

    if(root_node->children)
    find_value_by_key(doc, root_node->children, key);

    root_node = root_node->next;
    }

    return string("Nothing!");
    }

    I can find the element and it prints the value. but the return is
    always "Nothing!".
    i think i make a classical mistake, but have no idea why the function does
    not return the value of the element???
     
    Uwe Grawert, Mar 21, 2006
    #1
    1. Advertising

  2. It looks like the return syntax might be wrong.

    return string((char*) xmlNodeListGetString(doc,
    root_node->xmlChildrenNode, 1));

    This creates a temporary variable of type string which is destroyed when
    scope is gone.
    Try returning just the cast itself

    return (char*) xmlNodeListGetString(doc,
    root_node->xmlChildrenNode, 1);





    "Uwe Grawert" <> wrote in message
    news:p...
    >I have the following recursive function:
    >
    > string find_value_by_key (xmlDocPtr doc,
    > xmlNodePtr root_node,
    > const string& key)
    > {
    > while(root_node != NULL)
    > {
    > if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
    > cout << root_node->name << ": " << xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1);
    > return string((char*) xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1));
    > }
    >
    > if(root_node->children)
    > find_value_by_key(doc, root_node->children, key);
    >
    > root_node = root_node->next;
    > }
    >
    > return string("Nothing!");
    > }
    >
    > I can find the element and it prints the value. but the return is
    > always "Nothing!".
    > i think i make a classical mistake, but have no idea why the function does
    > not return the value of the element???
     
    Lattamore Osburn, Mar 21, 2006
    #2
    1. Advertising

  3. Uwe Grawert

    Uwe Grawert Guest

    Am Mon, 20 Mar 2006 18:41:32 -0800 schrieb Lattamore Osburn:

    > It looks like the return syntax might be wrong.
    >
    > return string((char*) xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1));
    >
    > This creates a temporary variable of type string which is destroyed when
    > scope is gone.
    > Try returning just the cast itself
    >
    > return (char*) xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1);
    >
    >
    >
    >
    >
    > "Uwe Grawert" <> wrote in message
    > news:p...
    >>I have the following recursive function:
    >>
    >> string find_value_by_key (xmlDocPtr doc,
    >> xmlNodePtr root_node,
    >> const string& key)
    >> {
    >> while(root_node != NULL)
    >> {
    >> if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
    >> cout << root_node->name << ": " << xmlNodeListGetString(doc,
    >> root_node->xmlChildrenNode, 1);
    >> return string((char*) xmlNodeListGetString(doc,
    >> root_node->xmlChildrenNode, 1));
    >> }
    >>
    >> if(root_node->children)
    >> find_value_by_key(doc, root_node->children, key);
    >>
    >> root_node = root_node->next;
    >> }
    >>
    >> return string("Nothing!");
    >> }
    >>
    >> I can find the element and it prints the value. but the return is
    >> always "Nothing!".
    >> i think i make a classical mistake, but have no idea why the function does
    >> not return the value of the element???


    But then it wouldn`t return "Nothing" !!! wouldn`t it?
     
    Uwe Grawert, Mar 21, 2006
    #3
  4. Uwe Grawert wrote:
    > Am Mon, 20 Mar 2006 18:41:32 -0800 schrieb Lattamore Osburn:
    >
    >> It looks like the return syntax might be wrong.
    >>
    >> return string((char*) xmlNodeListGetString(doc,
    >> root_node->xmlChildrenNode, 1));
    >>
    >> This creates a temporary variable of type string which is destroyed when
    >> scope is gone.
    >> Try returning just the cast itself
    >>
    >> return (char*) xmlNodeListGetString(doc,
    >> root_node->xmlChildrenNode, 1);
    >>
    >>
    >>
    >>
    >>
    >> "Uwe Grawert" <> wrote in message
    >> news:p...
    >>> I have the following recursive function:
    >>>
    >>> string find_value_by_key (xmlDocPtr doc,
    >>> xmlNodePtr root_node,
    >>> const string& key)
    >>> {
    >>> while(root_node != NULL)
    >>> {
    >>> if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
    >>> cout << root_node->name << ": " << xmlNodeListGetString(doc,
    >>> root_node->xmlChildrenNode, 1);
    >>> return string((char*) xmlNodeListGetString(doc,
    >>> root_node->xmlChildrenNode, 1));
    >>> }
    >>>
    >>> if(root_node->children)
    >>> find_value_by_key(doc, root_node->children, key);
    >>>
    >>> root_node = root_node->next;
    >>> }
    >>>
    >>> return string("Nothing!");
    >>> }
    >>>
    >>> I can find the element and it prints the value. but the return is
    >>> always "Nothing!".
    >>> i think i make a classical mistake, but have no idea why the function does
    >>> not return the value of the element???

    >
    > But then it wouldn`t return "Nothing" !!! wouldn`t it?
    >


    Are you using the Microsoft COM XML interface?
    If you are, aren't those 'xmlStrings' of type BSTR
    (int 16 wide chars)? If they are, then the xmlStrcmp()
    call will probably never match to the 8-bit char's used by
    the C++ 'string' class - and returned by key.c_str().

    Regards,
    Larry
     
    Larry I Smith, Mar 21, 2006
    #4
  5. Uwe Grawert

    GB Guest

    Uwe Grawert wrote:
    > I have the following recursive function:
    >
    > string find_value_by_key (xmlDocPtr doc,
    > xmlNodePtr root_node,
    > const string& key)
    > {
    > while(root_node != NULL)
    > {
    > if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
    > cout << root_node->name << ": " << xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1);
    > return string((char*) xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1));
    > }
    >
    > if(root_node->children)
    > find_value_by_key(doc, root_node->children, key);


    It looks like you are not using the value returned by this call to
    find_value_by_key. You should probably be comparing it to "Nothing!" and
    returning it if it is different.

    >
    > root_node = root_node->next;
    > }
    >
    > return string("Nothing!");
    > }
    >
    > I can find the element and it prints the value. but the return is
    > always "Nothing!".
    > i think i make a classical mistake, but have no idea why the function does
    > not return the value of the element???


    Gregg
     
    GB, Mar 21, 2006
    #5
  6. Uwe Grawert

    JE Guest

    Uwe Grawert wrote:
    > I have the following recursive function:
    >
    > string find_value_by_key (xmlDocPtr doc,
    > xmlNodePtr root_node,
    > const string& key)
    > {
    > while(root_node != NULL)
    > {
    > if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
    > cout << root_node->name << ": " << xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1);
    > return string((char*) xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1));
    > }
    >
    > if(root_node->children)

    {
    std:: string halfBakedRecursionReturn =
    > find_value_by_key(doc, root_node->children, key);

    if(halfBakedRecursionReturn != "Nothing!")
    {
    return halfBakedRecursionReturn;
    }
    }
    >
    > root_node = root_node->next;
    > }
    >
    > return string("Nothing!");
    > }
    >
    > I can find the element and it prints the value. but the return is
    > always "Nothing!".
    > i think i make a classical mistake, but have no idea why the function does
    > not return the value of the element???
     
    JE, Mar 21, 2006
    #6
  7. In message <>, Lattamore Osburn
    <> top-posted [corrected]
    >
    >"Uwe Grawert" <> wrote in message
    >news:p...
    >>I have the following recursive function:
    >>
    >> string find_value_by_key (xmlDocPtr doc,
    >> xmlNodePtr root_node,
    >> const string& key)
    >> {
    >> while(root_node != NULL)
    >> {
    >> if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
    >> cout << root_node->name << ": " << xmlNodeListGetString(doc,
    >> root_node->xmlChildrenNode, 1);
    >> return string((char*) xmlNodeListGetString(doc,
    >> root_node->xmlChildrenNode, 1));
    >> }
    >>
    >> if(root_node->children)
    >> find_value_by_key(doc, root_node->children, key);
    >>
    >> root_node = root_node->next;
    >> }
    >>
    >> return string("Nothing!");
    >> }
    >>
    >> I can find the element and it prints the value. but the return is
    >> always "Nothing!".
    >> i think i make a classical mistake, but have no idea why the function does
    >> not return the value of the element???

    >
    >
    >It looks like the return syntax might be wrong.
    >
    >return string((char*) xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1));
    >
    >This creates a temporary variable of type string which is destroyed when
    >scope is gone.


    Irrelevant. The function returns by value, not reference, so what it
    returns is a *copy* of the temporary.

    >Try returning just the cast itself
    >
    >return (char*) xmlNodeListGetString(doc,
    > root_node->xmlChildrenNode, 1);


    .... which constructs a temporary of the function's return type (i.e.
    string) and returns a copy of it. Not a lot of difference.

    The problem is elsewhere.

    --
    Richard Herring
     
    Richard Herring, Mar 27, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Erl

    Online-HTML-Editor kills images

    Stefan Erl, Aug 8, 2003, in forum: Perl
    Replies:
    1
    Views:
    955
    J├╝rgen Exner
    Aug 8, 2003
  2. Tom Welch
    Replies:
    1
    Views:
    350
    Chris Jackson
    Nov 21, 2003
  3. Lloyd Sheen

    Express Version Kills Current Version

    Lloyd Sheen, Jun 29, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    571
    Ken Cox [Microsoft MVP]
    Jul 2, 2004
  4. n00m
    Replies:
    12
    Views:
    1,122
  5. vamsi
    Replies:
    21
    Views:
    2,113
    Keith Thompson
    Mar 9, 2009
Loading...

Share This Page