First of all im new here so Hi all..

im also new to c/c++ have been a java php and actionscript developer for some time now and finally have time to learn something new so im gonna go for c++ im just tryna get familiar with everything but i ran into some (to me) weird results.

i was trying to make a recursive function but i kept getting errors so i stripped everything out of the function and still got an error can anyone explain me why the following fails everytime after calling the function for the 262058 time with a segmentation fault.

void recursive() {
	recursive();
}
int main( int argc, char **argv ) {
	recursive();
}

its probably very obvious and easy (just not to me)

thanks in advance,
Yrm

Nothing stops the recursion except when your system starts getting trashed.

so this is not going to work and if i need lots of repeating i should just create a iterative solution?

Whether iterative or recursive, the loop needs a stop condition.

If you recurse many thousands of times, perhaps an iterative solution would be safer.

But a recursive function in C++ would be no different to a recursive function in Java. There is no C++ magic here, almost every language can manage recursion.

But if you don't understand the need for a stop condition (as Dave points out in post #4), then you're stuck anyway.

If you translated what you wrote back into Java, it would still spin out of control until it ran out of some resource or other.

In recursion there are 2 main things :

1) basis :
2) Induction.

In you recursive, you have provided the Induction, namely
f() = f(), but you have not provided a basis case.

Here is an example of a recursion function. Again, a recursion function
has a basis and the Induction.

//example of recursion
void printXTimes(int printWhat, int howMany){
    if( howMany == 0) return; //our basis...

    cout << printWhat; //print whatever is passed in
    printXTimes(printWhat, howMany-1); //our Induction
}

As you can see we reduce howMany by 1 in ever recursive call.

The above function is a recursive function because it has a basis case
and an Induction. It is defined as follows :

Our function is defined as, F : N --> empty , where empty means it
returns nothing. Then our basis and induction are :

Basis : n = 0
Induction : F(x,n) = F(x,n-1)

commented: thx for the info +0

First of all im new here so Hi all..

im also new to c/c++ have been a java php and actionscript developer for some time now and finally have time to learn something new so im gonna go for c++ im just tryna get familiar with everything but i ran into some (to me) weird results.

i was trying to make a recursive function but i kept getting errors so i stripped everything out of the function and still got an error can anyone explain me why the following fails everytime after calling the function for the 262058 time with a segmentation fault.

void recursive() {
	recursive();
}
int main( int argc, char **argv ) {
	recursive();
}

its probably very obvious and easy (just not to me)

thanks in advance,
Yrm

Now you have learned something about malicious code, don't you?
It is like looping infinitely while creating new arrays without deleting them. Guess what? ;)

Ok just rumbling, solution is well pointed above :)

first of all thank you very very much for the great info, its been a while since i posted a question on any forum and got a useful answer!!!

As i explained the function used to contain some other stuff, one of them also a stop condition but because i had a segmentation fault i thought well lets just take everything out start over until i get it again and then i know where i went wrong its not the smartest way to debug but at least im really "seeing" where im going wrong.

I never really used a recursive function but now i know how to do it right if i need to.
But i guess i was just staring blind on trying to make a recursive function while i dont even need one.. I solved my problem by just creating a simple loop

for(;;) { if(error) return 0}

gr,
Yrm

Yup! And that loops infinitely if it finds no error :)

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.