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

  • Context: Comp Sci 
  • Thread starter Thread starter anonim
  • Start date Start date
  • Tags Tags
    Function Recursion
Click For Summary

Discussion Overview

The discussion centers around the issue of segmentation faults occurring in a recursive function due to the use of an uninitialized pointer. Participants explore the implications of recursion depth and pointer initialization in the context of a specific code snippet.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant presents a code snippet where an uninitialized pointer is used, leading to a segmentation fault during recursion.
  • Another participant questions the recursion depth, suggesting that the function may run until stack memory is exhausted.
  • A later reply challenges the initial code by asking about conditions under which the function might return without further recursion, indicating a lack of clarity in the provided code.
  • Concerns are raised about the assignment to an uninitialized pointer and the implications of using a pointer in this context.

Areas of Agreement / Disagreement

Participants express uncertainty regarding the behavior of the function and the role of the uninitialized pointer. There is no consensus on how to resolve the segmentation fault or the correctness of the code provided.

Contextual Notes

The discussion highlights missing assumptions about pointer initialization and the conditions under which recursion terminates. The implications of recursion depth and stack memory usage are also noted but remain unresolved.

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?
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K