简体   繁体   中英

How to access the call stack?

I have a tree data structure composed by parent nodes and child nodes, something like this:

<Node1>
  <Node2/>

  <Node3>
    <Node4/>
  </Node3>
</Node1>

And to parse the structure i use a μ-recursive function:

void RecursiveFunction(NodeT node)
{
    if(node.has_child())
        RecursiveFunction(node.child());
}

When i call RecursiveFunction passing as parameter, for example, Node2 i want to have the access to the father (Node1) data without storing these data using additional data structures, because the data of the first node has been stored on the call stack before making the recursive call. So I need to access the call stack to read data of Node1 while I am parsing Node2 failing direct access to the father, and so on.

For example, if i'm parsing Node4:

void RecursiveFunction(NodeT node)
{
    /* Access to Node3 and Node1 data...

    if(Node4.has_child())
        RecursiveFunction(Node4.child()); */
}

Is it possible? In what way?

Thanks.

It would be possible, but it would be quite dangerous to do so. This is mainly because you want to mess with the process stack. Also I don't think there are any library functions to do so, that being said you would have to do some pointer arithmetics to achieve that.

Even if you would find a way to access the data from the calling function, it would not be easy for any other programmer to understand what you do.

I would suggest to replace the recursion by an iteration which should allow you to express clearly what you want to do. See Way to go from recursion to iteration for more information.

Accessing the callstack is a little bit of overengineering for me. Convert the function from recursive to iterative and use your own stack. The stack shall contain an instance of NodeT and integer value informing the function, how many of the node's children has been already processed. If this value is lower than node's child count, loop shall increment this value and add the next child to the stack. Otherwise it shall process the node and then pop it from the stack.

The other advantage is, that you're no longer restricted by size of the program's stack and you can process way more nested structures if there is such a need. Otherwise you risk, well, let's not be affraid to say, stack overflow.

If you want or must (eg homework requirement?) stick with recursion, add the parent node as a second argument to your function which defaults to NULL , indicating that the current node is the root node. Ie:

void RecursiveFunction(NodeT const& node, NodeT const* parentPtr=NULL)
{
    if(node.has_child())
        RecursiveFunction(node.child(), &node);
}

Note that I modified your function to accept the first argument by reference to const, otherwise you create a lot of copies of your tree.

The call stack is used for function parameters as well*, so use that to build the chain:

struct chain {
  NodeT* node;
  chain const* parent;
}
void RecursiveFunction(chain const& c)
{
    if(c.node->has_child()) {
       chain child = { c.node->child(), &c };
        RecursiveFunction(child);
    }
}

[*] At least conceptually; actual implementations may differ. In any case this C++ code is portable, unlike low-level hacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM