473,786 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(*fun c)(double), double a, double(*next)(d ouble),
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(doubl e a, double b){
double result = 0;
sum(&cube, a, &inc, b, &result);
return result;
}

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

May 17 '07 #1
3 3190
we********@gmai l.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(*fun c)(double), double a, double(*next)(d ouble),
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********@gmai l.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********@gma il.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.securityfoc us.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
6812
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 works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
3
1939
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: /var/www/cgi-bin/script.cgi when i debug the program i get Segmentation fault gdb ./script.cgi
3
2125
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 be returned by gcc compiler on linux. What are the other "major" reasons to have "Segmentation Fault"? I hope this is not a stupid question and someone is will to give me some exampls(part of code) with errors a newer might esasily make and
3
11445
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
5
2998
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
18
26121
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)? Thanks.
7
5881
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 testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5187
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 from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
14
2671
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
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.