question

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ asked $$ANON_USER$$ answered

Getting error code exited with code -1073741571 while running a C program

Why am I getting error "exited with code -1073741571" while taking dec's value more than 4539 in the below mentioned C recursive program, but this code is resulting right answer upto the dec's value 4539. So please help me out.

include<stdio.h>

int sum(int);
int main()
{
int fsum, dec;
printf("Enter a number\n");
scanf_s("%d", &dec);
fsum = sum(dec);
printf("Total Sum=%d\n", fsum);
return 0;
}
int sum(int dec)
{
int rsum;

 if (dec == 1)
     return 1;
 else
     rsum=(sum(dec - 1) + dec);
 return rsum;    

}

c++
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What value did you enter in response to the prompt?

-1073741571 is the decimal equivalent of C00000FD which is the documented error code for stack overflows.

0 Votes 0 ·
WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

As others have noted, you're experiencing stack overflow.
Calling a function requires stack allocations for
parameter passing, storing return addresses, etc.

Recursion can result in using up all of the
available reserved stack size, which in VS is
1 MB by default. You can reserve more by setting
the reserve size in the project properties as already
described.

Another way to reserve a larger stack is via the pragma
directive. Try placing the following pragma at the
start of your program to reserve a 4 MB stack:

 #pragma comment(linker, "/STACK:4000000")

Note that regardless of the stack size there will
always be a limit which you may exceed if you do
deep enough recursion. Therefore recursion should
be replaced with other techniques if a very large
number of recursions is expected in normal use.

Note as well that other activities in a program will
also be using stack space, so the actual amount
available for recursion is dependent on the overall
program requirements. For example, a large local
static array may take up a lot of stack.

  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

Recursive programs require additional memory to organise the calls, this memory block is limited, and it seems that you achieved the limit. To increase it, go to Project Properties, Linker, System, “Stack Reserve Size” and try 2000000, for example.

Consider non-recursive algorithms too.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ answered

Thanks to all for helping me.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.