Thread: Segmentation fault - won't enter for loop!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    16

    Segmentation fault - won't enter for loop!

    Heres my relevant bits of code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define EXPECTED_ARGS   2
    #define INTEGRAL_STEPS  100
    #define LOG_2           log(2) 
    #define MAX_N           10
    
    
    int main(int argc, char* argv[])
    {
      double      integral_yn[MAX_N], recursive_yn[MAX_N];
      double      a;
      double      sum, x;
      int         valid_input;
      int         i, j, n;
      int         steps = INTEGRAL_STEPS;
      FILE       *output;
    .
    .
    .

    Code:
      /* Evaluate integral y_n */
      for(n=1; n<10; n++)
      {
        printf(" n = %d    ", n);
        integral_yn[n] = 0;
        printf("integral_yn began/n");
        for(i=0; i<steps; i++)
        {
          x = i/steps;
          sum = pow(x, n+1)/( (x + a)*steps ); 
          integral_yn[n] = integral_yn[n] + sum;
        }
        printf("integral_yn[%d] done/n", n);
      }
    I reach a segmenation fault just before entering the for loop, and the line printf(" n = %d ", n); doesn't even run!

    Whats wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps the error lies in the code that you did not show.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Well, I had a printf statement right before the for loop which does execute.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you sure that the segmentation fault happens before the for loop's first iteration?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Here is all of my code:





    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define EXPECTED_ARGS   2
    #define INTEGRAL_STEPS  100
    #define LOG_2           log(2) 
    #define MAX_N           10
    
    
    int main(int argc, char* argv[])
    {
      double      integral_yn[MAX_N], recursive_yn[MAX_N];
      double      a;
      double      sum, x;
      int         valid_input;
      int         i, j, n;
      int         steps = INTEGRAL_STEPS;
      FILE       *output;
    
    
      /* Get command line input */
      if(argc == EXPECTED_ARGS)
      {
        valid_input = sscanf(argv[1], "%lf", &a);
        printf("a stored\n");
      }
      else
      {
        printf("***Enter program name followed by a strictly positive number***\n");
        valid_input = 0;
      }
    
    
      /* Checks a strictly positive */
      if(a <= 0)
      {
        printf("***Number must be strictly positive***\n");
        valid_input = 0;
      }
    
    
      /* Final validation */
      if(!valid_input)
      {
        printf("Input validation failed!!!\n");
        return(EXIT_FAILURE);
      }
    
    
      /* Evaluate y_1 using integral */
      double y1 = 1 - a*LOG_2;
    
    
      integral_yn[0] = y1;
      printf("integral_yn[0] done\n");
      recursive_yn[0] = y1;
      printf("recursive_yn[0] done\n");
    
    
      /* Evaluate integral y_n */
      for(n=1; n<10; n++)
      {
        printf(" n = %d    ", n);
        integral_yn[n] = 0;
        printf("integral_yn began/n");
        for(i=0; i<steps; i++)
        {
          x = i/steps;
          sum = pow(x, n+1)/( (x + a)*steps ); 
          integral_yn[n] = integral_yn[n] + sum;
        }
        printf("integral_yn[%d] done/n", n);
      }
    Sorry for the length. The line printf("recursive_yn[0] done\n"); does print, but the next one does not - I get a segmentation fault

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    is there something wrong with this line?

    Code:
         integral_yn[n] = integral_yn[n] + sum;

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, I have not been able to discern your error by eyeballing the code, and then actually compiling and running it does not reproduce the segmentation fault for me.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    No seg fault here either. You do have a logic problem on line 70 though. i / steps always equals 0.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I see no problem either. I suspect the error is on line 76 or later, posibly using that variable 'output' that would otherwise just be unused, especially considering that main is otherwise missing a closing brace.

    In other words, that is not all of your code, you're still not showing the part with the error! Stop beating around the bush; Select-All ... Copy ... Paste, is it really so hard?
    Last edited by iMalc; 11-30-2011 at 02:23 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Lolz. I'm just a little wary about giving putting my whole assignment on the internet. I'll get screwed if one of my classmates copies it and corrects the error. I'll put the code up later though, I'm on my phone.

    I don't understand why i / steps always equals 0 though, i goes from 0 to 99 and steps = 100 right?

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Oh wait, it's because i and steps are int!

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Full code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define EXPECTED_ARGS   2
    #define INTEGRAL_STEPS  100
    #define LOG_2           log(2) 
    #define MAX_N           10
    
    
    int main(int argc, char* argv[])
    {
      double      integral_yn[MAX_N], recursive_yn[MAX_N];
      double      a;
      double      sum, x;
      int         valid_input;
      int         i, j, n;
      int         steps = INTEGRAL_STEPS;
      FILE       *output;
    
    
      /* Get command line input */
      if(argc == EXPECTED_ARGS)
      {
        valid_input = sscanf(argv[1], "%lf", &a);
        printf("a stored\n");
      }
      else
      {
        printf("***Enter program name followed by a strictly positive number***\n");
        valid_input = 0;
      }
    
    
      /* Checks a strictly positive */
      if(a <= 0)
      {
        printf("***Number must be strictly positive***\n");
        valid_input = 0;
      }
    
    
      /* Final validation */
      if(!valid_input)
      {
        printf("Input validation failed!!!\n");
        return(EXIT_FAILURE);
      }
    
    
      /* Evaluate y_1 using integral */
      double y1 = 1 - a*LOG_2;
    
    
      integral_yn[0] = y1;
      printf("integral_yn[0] done\n");
      recursive_yn[0] = y1;
      printf("recursive_yn[0] done\n");
    
    
      /* Evaluate integral y_n */
      for(n=1; n<10; n++)
      {
        printf(" n = %d    ", n);
        integral_yn[n] = 0;
        printf("integral_yn began/n");
        for(i=0; i<steps; i++)
        {
          x = i/steps;
          sum = pow(x, n+1)/( (x + a)*steps ); 
          integral_yn[n] = integral_yn[n] + sum;
        }
        printf("integral_yn[%d] done/n", n);
      }
      printf("integral_yn done/n");
      /* Evaluate recursive y_n */
      for(j=1; j<10; j++)
      {
        recursive_yn[j] = 1/(j+1) - a*recursive_yn[j-1];
      }
      printf("recursive_yn done/n");
      /* Prints to file assign4.out */
      output = fopen("assign4.out", "w");
    
    
      fprintf(output, "a = %.4lf, y_1 = %.6lf\n", a, integral_yn[0]);
      
      for(i=1; i++; i<10)
      {
        fprintf(output, "%10.9lf   %10.9lf", integral_yn[i], recursive_yn[i]);
      }
    
    
      fclose(output);
      return(0);
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Want a couple of hints?

    First... All of this...
    Code:
      /* Get command line input */
      if(argc == EXPECTED_ARGS)
      {
        valid_input = sscanf(argv[1], "%lf", &a);
        printf("a stored\n");
      }
      else
      {
        printf("***Enter program name followed by a strictly positive number***\n");
        valid_input = 0;
      }
    
    
      /* Checks a strictly positive */
      if(a <= 0)
      {
        printf("***Number must be strictly positive***\n");
        valid_input = 0;
      }
    
    
      /* Final validation */
      if(!valid_input)
      {
        printf("Input validation failed!!!\n");
        return(EXIT_FAILURE);
      }
    ... can be reduced to...
    Code:
      /* Get command line input */
      if((argc < EXPECTED_ARGS) || (sscanf(argv[1], "%lf", &a) < 1) || (a < 1))
       { printf("Error: Invalid input"); 
          return(EXIT_FAILURE); }
    There are other simplifications you can use in your code as well...
    such as assigning the constant INTEGRAL_STEPS to the steps variable, even though it's value never changes... just use INTEGRAL_STEPS in lines 68, 70 and 71 and discard the steps variable entirely.


    Generally you should always look for ways to simplify things... with needless complexity comes errors.
    Last edited by CommonTater; 11-30-2011 at 06:49 AM.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    printf("integral_yn began/n");
    The above is wrong "/n" should be "\n".

    Tim S.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Or even just compile the damn thing with more warnings.
    Code:
    C:\Users\sc\Documents\Projects\forum\main.c||In function 'main':|
    C:\Users\sc\Documents\Projects\forum\main.c|89|warning: statement with no effect|
    ||=== Build finished: 0 errors, 1 warnings ===|
    viz
    Code:
      for(i=1; i++; i<10)
      {
        fprintf(output, "%10.9lf   %10.9lf", integral_yn[i], recursive_yn[i]);
      }
    The "no effect" is i < 10
    what this loop does however is increment i ad nauseum until it barfs off the end of memory.

    > I'll get screwed if one of my classmates copies it and corrects the error.
    But you should win by showing you published first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault when entering a for loop
    By rehpot in forum C Programming
    Replies: 6
    Last Post: 02-17-2011, 09:59 PM
  2. Replies: 9
    Last Post: 02-22-2009, 11:50 PM
  3. segmentation fault in for loop
    By Rpog in forum C Programming
    Replies: 3
    Last Post: 04-20-2004, 06:21 AM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM
  5. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 01:23 AM