5 Replies - 317 Views - Last Post: 25 October 2013 - 10:03 AM Rate Topic: -----

#1 desinerd93  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 13-September 13

segmentation fault!

Posted 25 October 2013 - 05:49 AM

Hi guys! I wrote this program and it compiles but when i try to run the program, I get the error message that the
Segmentation fault (core dumped)
. Here is my code:
#include<iostream>
using namespace std;
int fib(int n){
    if (n == 0&& n==1 )
        return 1;
    int p = fib(0);
    int c = fib(1);
    int buff = 0;
    for (int i=1; i < n; ++i) {
      buff = c;
      c = p+c;
      p = buff;
    };
    return c;
}
int main(){
 for(int i=1; i<70; i++)
 cout<<" fib("<<i<<") = "<<fib(i)<<endl;
}



Is This A Good Question/Topic? 0
  • +

Replies To: segmentation fault!

#2 Peter O  Icon User is online

  • D.I.C Head

Reputation: 75
  • View blog
  • Posts: 177
  • Joined: 19-October 13

Re: segmentation fault!

Posted 25 October 2013 - 05:58 AM

n can't be 0 and 1 at the same time, can it? ;)
Was This Post Helpful? 0
  • +
  • -

#3 desinerd93  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 63
  • Joined: 13-September 13

Re: segmentation fault!

Posted 25 October 2013 - 06:06 AM

ok, so i fixed that but now this is the output:

fib(1) = 1
fib(2) = 1
Segmentation fault (core dumped)
here is the fixed code:

#include<iostream>
using namespace std;
int fib(int n){
    if (n == 1)
       return 1;
    if (n==2 )
       return 1;
    int p = fib(0);
    int c = fib(1);
    int buff = 0;
    for (int i=1; i < n; ++i) {
      buff = c;
      c = p+c;
      p = buff;
    };
    return c;
}
int main(){
 for(int i=1; i<70; i++)
 cout<<" fib("<<i<<") = "<<fib(i)<<endl;
}


This post has been edited by no2pencil: 25 October 2013 - 06:11 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5170
  • View blog
  • Posts: 26,857
  • Joined: 10-May 07

Re: segmentation fault!

Posted 25 October 2013 - 06:19 AM

Why are you initializing p & c to run the command they are in?

    //int p = fib(0);
    //int c = fib(1);
    int p = 0, c = 1;

[/code]

With the above I get the following out put:

Quote

fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
fib(10) = 55
fib(11) = 89
fib(12) = 144
fib(13) = 233
fib(14) = 377

Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: segmentation fault!

Posted 25 October 2013 - 08:22 AM

Just to explain, line 8 causes your method to call your Fib method, which when it reaches line 8 calls your Fib method, which when it reaches line 8 calls your Fib method, ... You see where this is going?
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3458
  • View blog
  • Posts: 10,665
  • Joined: 05-May 12

Re: segmentation fault!

Posted 25 October 2013 - 10:03 AM

That was just kind of interesting to see the error being reported was "Segmentation fault". I thought that newer versions of *nix and Linux now report "Stack overflow" rather than "Segmentation fault".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1