Hello ,
The following function takes a number x and returns the sum of all numbers before n.
For ex. when x = 5 the function should return 4+3+2+1.

unsigned long long rep(int x)
{
    if(x==1)return 1;
    return x + (rep(x-1)%314159);
}

I'm getting a segmentation fault with the above code when i pass 1,000,000 as a parameter to the function "rep".

Any help would be greatly appreciated , Thanks in advanced.

Recommended Answers

All 2 Replies

MSVS for example sets a default stack size of one megabyte. So I guess you would be limited to less than 64k or 128k recursions (64/32 bit OS) since every recursion needs space for x and a pointer on the stack.

I see now , Thanks a lot.

Be a part of the DaniWeb community

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