473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Can recursion cause segmentation fault?

Hi,

I was trying to implement a higher order function in C, summation over
a range with a given function. And the summation function is then used
to define sum_cubes. The program works fine when VAR is below 30000 or
around that, but as soon as VAR is bigger, say 40000, or 50000, then I
receive a segmentation error. Can anyone tell me why this is
happening? Thanks in advance.

#include <stdio.h>
#define VAR 30000
double sum(double(*func)(double), double a, double(*next)(double),
double b, double *result){
/*summation over func from a to b as incremented by next*/
if(a b)
return 0;
else{
*result += (*func)(a);
return sum(func, (*next)(a), next, b, result);
}
}

double inc(double n){
return (++n);
}

double cube(double n){
return n*n*n;
}

double sum_cubes(double a, double b){
double result = 0;
sum(&cube, a, &inc, b, &result);
return result;
}

int main(){
printf("sum_cubes(1, %d) = %d\n", VAR, sum_cubes(1, VAR));
return 0;
}

May 17 '07 #1
3 3166
we********@gmail.com wrote:
Hi,

I was trying to implement a higher order function in C, summation over
a range with a given function. And the summation function is then used
to define sum_cubes. The program works fine when VAR is below 30000 or
around that, but as soon as VAR is bigger, say 40000, or 50000, then I
receive a segmentation error. Can anyone tell me why this is
happening? Thanks in advance.

#include <stdio.h>
#define VAR 30000
double sum(double(*func)(double), double a, double(*next)(double),
double b, double *result){
Can't you give the variables better names than `a` and `b`?
`from` and `to`, for example?
/*summation over func from a to b as incremented by next*/
if(a b)
return 0;
else{
*result += (*func)(a);
(You can write that as `func(a)` if you prefer. I do, but some
people want to make it explicit that they're calling through
an explicit function pointer.)
return sum(func, (*next)(a), next, b, result);
Why are you always returning `0`? If you're going to deliver
the result by side-effecting `*result`, why have a function
result at all -- conversely, why not return the answer at the
bottom of the recursion, rather than some integer?
}
}
You've probably blown the stack. Even though this code is
blatently tail-recursive, C compilers are not required to
optimise tail-calls of any kind. Since is /is/ so obviously
tail-recursive, it's easy to turn it into a loop.
double inc(double n){
return (++n);
}
Please, just `return n+1`. There's no point in side-effecting the
local variable `n`.
int main(){
Yay! `int main`! (That's approval, by the way.)

--
"It took a very long time, much longer than the most /Sector General/
generous estimates." - James White

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 17 '07 #2
we********@gmail.com said:
Hi,

I was trying to implement a higher order function in C, summation over
a range with a given function. And the summation function is then used
to define sum_cubes. The program works fine when VAR is below 30000 or
around that, but as soon as VAR is bigger, say 40000, or 50000, then I
receive a segmentation error.
That is one legal outcome of passing a double to printf to match a %d
character. But it does sound very much as if you're blowing your
function stack.

If you must use recursion, consider splitting the problem into two parts
and recursing into each part separately. For example, you might deal
with a to (a+b)/2 in one half, and e+(a+b)/2 to b in the other half.
This means that instead of recursing VAR levels deep, you'll only be
recursing log2 VAR levels instead.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 17 '07 #3
"we********@gmail.com" wrote:
>
I was trying to implement a higher order function in C, summation
over a range with a given function. And the summation function is
then used to define sum_cubes. The program works fine when VAR is
below 30000 or around that, but as soon as VAR is bigger, say
40000, or 50000, then I receive a segmentation error. Can anyone
tell me why this is happening? Thanks in advance.
Assume the system uses a stack. What is it supposed to do when the
space for that stack is exhausted? Maybe it can't enlarge it.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
14
by: asit | last post by:
#include <stdio.h> int main() { int i; for(i=1;i<=10;i++) main(); printf("C is urs.."); return 0; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.