Comp Sci Why Does Using an Uninitialized Pointer Cause a Segmentation Fault in Recursion?

AI Thread Summary
Using an uninitialized pointer in the code leads to a segmentation fault because it attempts to dereference a pointer that has not been assigned a valid memory address. The function `foo` is designed to call itself recursively, but it lacks a base case to terminate the recursion, which can result in excessive stack usage. The discussion highlights the need for a proper return condition to prevent infinite recursion. Additionally, the pointer `s` is incorrectly used, as it points to an undefined location, causing further issues. Proper initialization and a clear exit strategy are essential to avoid segmentation faults in recursive functions.
anonim
Messages
39
Reaction score
2
Homework Statement
Calculate the Math Formula
Relevant Equations
-
C:
double foo(int arr[], double *ave, int index){
  double *s;
  *s=*ave;
    // calculation//
    return(foo (arr,ave,index));
// other calculation//
}

I want to keep the ave value during the recursion, because after ave is calculated, I will do another calculation is recursively in this function. But I write like this, I am getting segmentation fault.
 
Physics news on Phys.org
Hint: How many levels deep will the recursive calls go in your code?
 
anorlunda said:
Hint: How many levels deep will the recursive calls go in your code?
I did not understand what you say. the function will call itself about 560 times
 
anorlunda said:
Hint: How many levels deep will the recursive calls go in your code?
anonim said:
I did not understand what you say. the function will call itself about 560 times
Based on what you posted, the function will run until there is no more stack memory for it.
anonym said:
C:
double foo(int arr[], double *ave, int index){
  double *s;
  *s=*ave;
    // calculation//
    return(foo (arr,ave,index));
// other calculation//
}

I want to keep the ave value during the recursion, because after ave is calculated, I will do another calculation is recursively in this function. But I write like this, I am getting segmentation fault.
Right, and @anorlunda's hint might have something to do with why you're getting this fault.
Are there any conditions in which the function returns without calling itself? You don't show any in this code snippet. In particular, how does the part you have commented as "other calculation" actually get executed?
 
anonim said:
Homework Statement:: Calculate the Math Formula
Relevant Equations:: -

C:
double foo(int arr[], double *ave, int index){
  double *s;
  *s=*ave;
    // calculation//
    return(foo (arr,ave,index));
// other calculation//
}

I want to keep the ave value during the recursion, because after ave is calculated, I will do another calculation is recursively in this function. But I write like this, I am getting segmentation fault.

You're assigning to the address of an uninitialised pointer:
C:
  double *s;
  *s=*ave;
Why is s a pointer here?
 
Back
Top