LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   continuation (https://www.linuxquestions.org/questions/programming-9/continuation-634431/)

cleopard 04-10-2008 11:38 AM

continuation
 
I've been trying to understand the concept of continuation passing style and I get it to a point. But most of the code examples I find are in languages that I'm not familiar with, such as Scheme. CPS can apparently be used with C and if I could find some code examples in C, that would help a lot. And if anyone could make book suggestions (I've done some searching on Safari and Amazon, and saw a couple that might deal with this topic on some level) or any other sources, that would be great, too.

ta0kira 04-10-2008 12:16 PM

I think making it work in C would also require a return statement, or just void functions:
Code:

void function2(int aArg)
{
    //...
}
 
void function1(int aArg)
{
    //...
    function2(aArg);
}
 
int main()
{
    //...
    int control;
 
    function1(control);
}

C isn't designed well for that because while function2 is running, the items on the stack belonging to function1 and main are still there, even though you don't intend to use them again.
ta0kira

Edit: You can overcome the stack problem like this:
Code:

void function1(int aArg)
{
    {
    //...
    }
 
    function2(aArg);
}

There will be an argument left on the stack for each time control is passed, but at least that's uniform and predictable.

PS I just learned this in response to your question, so I may have misinterpreted the concept.


All times are GMT -5. The time now is 08:17 PM.