I'm getting infinite recursion here. I'm sorry if this is obvious but I don't see what I'm doing wrong here. program() gets called from main, then program() calls declaration_list(), then declaration_list() calls declaration(). I don't see how in the function declaration_list it ever calls declaration_list() again when right before that it calls declaration(), and declaration() never calls declaration_list().

void declaration()
{
    printf("declaration().\n");
}

void declaration_list()
{
    printf("declaration_list().\n");
    declaration();
    declaration_list();
}

void program()
{
    printf("In program().\n");
    declaration_list();
}

Recommended Answers

Program calls declaration_list(), declaration_list() calls declaration(), which prints something and returns, then declaration_list() calls declaration_list(), it's self again, which proceeds to call declaration(), then we go back to execute the last line of declaration_list() again...

Trace through it a line at a time, essentially declaration_list() calls it's self once an …

Jump to Post

All 4 Replies

Program calls declaration_list(), declaration_list() calls declaration(), which prints something and returns, then declaration_list() calls declaration_list(), it's self again, which proceeds to call declaration(), then we go back to execute the last line of declaration_list() again...

Trace through it a line at a time, essentially declaration_list() calls it's self once an execution, so it is in fact infinite. There is no breaking out of this loop. Since there is no way of breaking out, the stack gets too large, and an exception is thrown/executable dies, or the runtime detects a certain number of recurses, and chokes. This is C right?

This is C right?

With any luck it's C++ as best practice in C is to specify void as the parameter list. But yes, there's no termination condition in declaration_list to stop the recursion, so it'll go until the machine says "uncle".

commented: IE, the stack overflows! +13

And BTW, a stack overflow like this is a perfect attack vector for malware!

Indeed, C/C++ has some awesome features like that... lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.