ARUN KUMAR
ARUN KUMAR

Reputation: 3

How can I achieve a stackframe from not getting destroyed after returning from the function?

I am trying to store elements for Fibonacci series in the array which is inside the function and after returning from function by "return arr " my stack frame is getting destroyed and I am unable to receive values in main function. I wanted to use recursion only and printing from main function these are my conditions.

#include <stdio.h>

int *fib(int *num, int *first, int *second, int i, int *arr);

int main()
{
  int num;
  printf("Enter any number : \n");
  scanf("%d", &num);
  int first = 0, second = 1, i = 0;
  int arr[num];
  int *result = fib(&num, &first, &second, i, arr);
  for (int i = 0; i < num; i++)
    printf("%d ", result[i]);
}

int *fib(int *num, int *first, int *second, int i, int *arr)
{
  int temp;
  if (*first == 0)
    printf("%d ", *first);
  if (*num < 0)
  {
    if (*second == 1)
      printf("%d ", *second);
    temp = *first - *second;
    *first = *second;
    *second = temp;
    if (*second > *num && *second < -*num)
    {
      *(arr + i) = *second;
      return fib(num, first, second, i++, arr);
    }
    else
      return arr;
  }
  else
  {
    temp = *first + *second;
    *first = *second;
    *second = temp;

    if (*second >= *num + 3)
      return arr;
    else
    {
      *(arr + i) = *second;
      return fib(num, first, second, i++, arr);
    }
  }

  printf("\n");
}

Upvotes: 0

Views: 83

Answers (5)

hgjhttt
hgjhttt

Reputation: 1

int *fib(int *num, int *first, int *second, int i, int *arr)
{
  int temp;
  if (*first == 0)
    printf("%d ", *first);
  if (*num < 0)
  {
    if (*second == 1)
      printf("%d ", *second);
    temp = *first - *second;
    *first = *second;
    *second = temp;
    if (*second > *num && *second < -*num)
    {
      *(arr + i) = *second;
      return fib(num, first, second, i++, arr);
    }
    else
      return arr;
  }
  else
  {
    temp = *first + *second;
    *first = *second;
    *second = temp;

Upvotes: 0

ARUN KUMAR
ARUN KUMAR

Reputation: 3

 #include <stdio.h>       



 int fib ( int *num , int *first , int *second , int count , int *arr ) ;      


 int main() 

 {

 char ch;

 do

 {
    int num = 0;

    printf("Enter any number : \n");

    scanf("%d", &num);

    int first = 0, second = 1, count = 0,size = num;           

if(num < 0)
    {
        size = -(num);                                 
    }
    int arr[size];                                          
    count = fib(&num, &first, &second, count, arr);        
    for (int j = 0; j < count; j++)
    {
        printf("%d ", *(arr + j));                      
    }
    printf("\n");
    printf("Do you want to repeat ? Y / N\n");              
    scanf("\n\n%c", &ch);
  } while (ch == 'y' || ch == 'Y');                             
}                                                                 

int fib(int *num, int *first, int *second, int count, int *arr)   
{

 int temp;
 if (*first == 0)
 {
    printf("%d ", *first);                                  
 }
 if (*num < 0)                                               
 {
    if (*second == 1)                                       
    {
        printf("%d ", *second);
    }
    temp = *first - *second;                                
    *first = *second;
    *second = temp;
    if (*second >= *num && *second <= -*num)
    {
        *(arr + count) = *second;                      
        return fib(num, first, second, count+1, arr);  
    }
 }
  else if(*num > 0)                                        
 {
    temp = *first + *second;
    *first = *second;                                    
    *second = temp;
    if (*second == 1)
    {
        printf("%d ", *second);                        
    }
    if (*second <= *num)
    {
        *(arr + count) = *second;                      
        return fib(num, first, second, count+1, arr);  
    }
  }
  return count;                                           
 }

Upvotes: 0

Patrick
Patrick

Reputation: 368

You can definitely achieve what you're wanting do without protecting the information on the function stackframes, as I don't believe your error has anything to do with that. With that said, recursion with Fibonacci numbers is kind of pointless compared to the iterative approach, it just ends up spamming extra stackframes when none are needed.

I took inspiration from Jabberwocky's answer and made an example with "recursion", though it ends up just being a fancy for loop.

#include <stdio.h>

void fib(int num, int *arr, int pos);

int main()
{
  int num;
  printf("Enter any number : \n");
  scanf("%d", &num);

  int arr[num];
  if (num > 1) {
    arr[0] = 0 ;
    arr[1] = 1 ;
  }
  int pos = 2;
  fib(num, arr, pos);

  for (int i = 0; i < num; i++)
    printf("%d ", arr[i]);
}

void fib(int num, int *arr, int pos)
{
  if (pos < num && pos > 1)
  {
    arr[pos] = arr[pos - 2] + arr[pos - 1];
    fib(num, arr, pos + 1);
  }  
  else {
      return;
  }
}

This stores all values into arr which is allocated in main, so deletion of stackframes has no effect. The fib function even when recursive does not need to return anything because it has the array pointer and can directly change the values of the array. Of course more error checking is needed but I think this is sufficient to get the idea across.

Upvotes: 1

Jabberwocky
Jabberwocky

Reputation: 50831

Somewhat off topic, but you probably simply want this:

#include <stdio.h>

void fib(int num, int *arr);

int main()
{
  int num;
  printf("Enter any number : \n");
  scanf("%d", &num);

  int arr[num] = { 0, 1 } ;
  fib(num, arr);

  for (int i = 0; i < num; i++)
    printf("%d ", arr[i]);
}

void fib(int num, int *arr)
{
  for (int i = 0; i < num; i++)
  {
    arr[i + 2] = arr[i] + arr[i + 1];
  }  
}

Using recursion is pretty pointless here. This is untested code, there may be bugs.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385264

You can't.

You are trying to fight against the very definition of what a stack frame is, and you are losing.

And you will always lose!

Instead, properly structure your program to pass data around in the way you need, in accordance with the rules and specifications of the language.

Upvotes: 4

Related Questions