Segfault

This is a discussion on Segfault within the C Programming forums, part of the General Programming Boards category; This code compiles fine but when I run it I'm getting a segmentation Fault. I'm not sure why. The program ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Segfault

    This code compiles fine but when I run it I'm getting a segmentation Fault. I'm not sure why. The program is suppose to Add two numbers without using the + operator.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int add(int a,int b);
     
    int main( int argc, char *argv[] ){
    
      int x;
      int y;
    
      x = atoi(argv[1]);
      y = atoi(argv[2]);
    
      printf("The sum of x and y is %d", add(x,y));
    
      return 0;
    
    }
    
    int add(int a, int b){
    
      if( a == b) return (a);
    
      return(add( ++a , --b ) );
    
    }

    Any Help would be greatly appreciated.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    the way i see it, when you do this
    Code:
    return(add( ++a , --b ) );
    theres no end to this recursive call.for eg when 3 and 2 are to be added, then this recursive call has no end because a is never getting equal to b.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    There is also the slight inconvenience that incrementing is an addition operation (as is decrementing, with somewhat more scope for liberal interpretation).

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    instead of
    if( a == b) return (a);

    should be

    if( 0 == b) return (a);

    also it is good idea check that b >=0 before processing
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And even if the code works for small numbers, it will NEVER work for large numbers (about half a million or so would be the max, depending a little bit on the processor, OS and optimization levels [unless the compiler figures out that it's tail recursion and optimizes the recursion away completely - which is possible].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Yes. Too deep reqursion will get you in troubles. Hence I would suggest you to think bitwise operations in loop. Basically you can do the summing with AND, XOR and shift to left.

    I'll leave the rest for you to ponder

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Segfault with additional variable?
    By misterFry in forum C++ Programming
    Replies: 11
    Last Post: 11-12-2008, 09:55 AM
  3. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 11:24 AM
  4. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 12:38 PM
  5. Segfault and Warning help
    By Uncle Rico in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21