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

In summary: Why not just a double?In summary, the code snippet shows a function named foo that takes in an array, a pointer to a double value, and an index as parameters. The function performs some calculations and returns a recursive call to itself. The goal of the function is to calculate a mathematical formula. However, there are potential issues with the code, including a possible segmentation fault due to assigning a value to an uninitialised pointer. Additionally, it is unclear how the "other calculation" part of the code is executed. One suggestion is to use a double variable instead of a pointer in this case.
  • #1
anonim
40
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
  • #2
Hint: How many levels deep will the recursive calls go in your code?
 
  • #3
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
 
  • #4
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?
 
  • #5
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?
 

Related to Why Does Using an Uninitialized Pointer Cause a Segmentation Fault in Recursion?

1. What is recursion in programming?

Recursion is a programming technique where a function calls itself repeatedly until a certain condition is met. It is a way to solve complex problems by breaking them down into smaller, simpler versions of the same problem.

2. How does recursion work?

When a recursive function is called, it first checks if the stopping condition is met. If not, the function calls itself again with a modified input. This process continues until the stopping condition is reached, and then the function starts returning values from the innermost call back to the initial call.

3. What is a double recursion function?

A double recursion function is a recursive function that calls itself twice within its body. This means that the function will have two recursive calls, which can lead to a more complex and efficient solution to certain problems.

4. What are the advantages of using recursion in programming?

Recursion allows for more elegant and concise code, as it can solve complex problems with fewer lines of code compared to iterative solutions. It also helps in breaking down complex problems into smaller, more manageable parts, making it easier to understand and debug.

5. What are some common pitfalls of using recursion?

One common pitfall is the risk of infinite recursion, where the stopping condition is not properly defined or not met, causing the function to call itself infinitely. This can lead to a stack overflow error. Additionally, recursive solutions may not always be the most efficient, and they can be harder to debug compared to iterative solutions.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
687
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
747
Back
Top