C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# C recursive function for product receiving segmentation fault

By : Lockito Marks
Date : November 20 2020, 11:01 PM
This might help you I think instead of taking the mid.You can consider doing like this too check the following code
code :
``````int product(int a, int b)
{

if (a >= b)
return a;
else {

return a*product(a+1,b);
}
}

int main() {

int p;
p = product(5,10);
printf("product is %d\n", p);

return 0;

}
``````

Share :

## Segmentation fault in recursive function

By : nofilikewifi
Date : March 29 2020, 07:55 AM
wish helps you I recently read about memoization for the first time(I'm a noob) and I wanted to try and make a fibonacci function that uses memoization. This is what I've tried, but anything over 1 just gives me a segmentation fault. Any help is appreciated!
code :
``````vector<unsigned int> fibvector;
``````
``````static vector<unsigned int> fibvector(MAXELEMENTS);
``````
``````if(fibvector[n] != 0) return fibvector[n];
``````
``````unsigned int fibonacci( unsigned int n )
{
static vector<unsigned int> fibvector;
unsigned int fib;

if ( fibvector.size() > n )
return fibvector[n];
if(n <=1){
fib = n;
}
else{
unsigned int v2 = fibonacci( n-2 );
unsigned int v1 = fibonacci( n-1 );
fib = v2 + v1;
}
fibvector.push_back(fib);
return fib;
}
``````

## Segmentation Fault with Recursive Function

By : Lungelo
Date : March 29 2020, 07:55 AM
seems to work fine I am very new to programming in C, and can't seem to locate the cause of the segmentation error that I have been getting. The program I wrote is as follows:
code :
``````if(x=0){...}
``````
``````if (x = 0)

is the same as:

x = 0; if (x)
``````

## Why does my recursive function cause a segmentation fault?

By : Pavel Bondar
Date : March 29 2020, 07:55 AM
I wish this helpful for you You are accessing memory you can't access. It just isn't obvious in the code (like a dangling pointer). Recursion is tricky; you can easily overflow the stack. The stack stores information when you enter a function (registers or addresses of parameters, saved values of registers used in the calling function and the called function, and a return pointer (where to jump to when you finish the function) basically.
The stack has a certain size. It might be quite large, but it is finite. In recursion, you keep calling the same function from within itself. If this happens too many times, you will "overflow the stack" -- that is, try to "push" more info onto the stack when it is already full, which means at an address past the end of the stack -- memory you may not have access to. (If you do have access to it, you maybe overwriting one of your variables or some such.)

## Segmentation fault in C++ recursive function

By : paleywiener
Date : March 29 2020, 07:55 AM
will help you You are indexing out of range. if i == s.length()-1 (which is the last character's index), then your s[i+1] will be out of range.
Also just in general the logic is flawed because you will restart the operation from the beginning each time you encounter matching. If you have 'abccba' I assume you would want 'abcba', however your code would return abca. The time complexity of your solution is less than ideal. This could be done in linear time.

## Why am I receiving segmentation fault in my insert function of a tree?

By : user01231
Date : March 29 2020, 07:55 AM
I wish this helpful for you The segmentation fault occurs in the insert function specifically at the *root = x; in the first if statement. The functions were already laid out and we just had to implement them so I cant add another parameter or anything. Ive been stuck on this for a while and have no idea what it could be. The root pointer was allocated memory so I really dont know what the problem is. Any help is appreciated, thanks. , This should be obvious: