Thread: Seg Fault on scanf

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    Seg Fault on scanf

    Hi everyone,

    I'm trying to implement Eratosthenes Sieve and I am getting a seg fault on my first scanf function. I'm very new to C and am just using nano to write the code and gcc to compile it. Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void PrintPrimes(const int Max)
    {
      int i = 0, j = 0;
      int* PossiblePrimes = (int*) malloc((Max - 2) * sizeof(int));
      printf("Test 2");
      if(!PossiblePrimes)
      {
        printf("Error: Out of Memory");
        return;
      }
      printf("Test 3");
      for(i = 0; i < Max - 2; i++)
      {
        *(PossiblePrimes + i) = i + 2;
      }
      printf("Test 4");
      i = 0;
      
      while(i < Max - 2)
      {
        if(PossiblePrimes[i] != 0)
        {
          for(j = 0; PossiblePrimes[i] * j < Max - 2; j++)
          {
            PossiblePrimes[(i * j) - 2] = 0;
          }
        }
        i++;
      }
      
      for(i = 0; i < Max - 2; i++)
      {
        if(PossiblePrimes[i] != 0)
          printf("&#37;d\n", PossiblePrimes[i]);
      }
      
      free(PossiblePrimes);
      
      return;
    }
    
    int main()
    {
      int UserMax = 0;
      
      scanf("%d", &UserMax);
      printf("Do I get to here?");
    
      if(UserMax >= 2)
        PrintPrimes(UserMax);
        
      return 0;
    }
    Never mind if my algo isn't quite right yet, the problem is the printf in main never executes and instead I get a seg fault Any idea or hints as to why that might be? I'm really stuck!

    EDIT: It only seg faults if I input a number greater than 10!?

    Thanks for your time, much appreciated.
    James

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It gets there, the seg fault occurs in the called function. Add the following after the printf:
    Code:
      fflush(stdout);
    And also think about adding newlines.

    As to the real issue, look in here:
    Code:
      while(i < Max - 2)
      {
        if(PossiblePrimes[i] != 0)
        {
          for(j = 0; PossiblePrimes[i] * j < Max - 2; j++)
          {
            PossiblePrimes[(i * j) - 2] = 0;
          }
        }
        i++;
      }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Ohh I see, so the problem in the called function happens before the output is flushed to the screen. Thanks for that.

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by jimmymax View Post
    Ohh I see, so the problem in the called function happens before the output is flushed to the screen. Thanks for that.
    Exactly - it's basically the compiler rearranging things a bit to optimize.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not exactly. The screen is only (usually) guaranteed to be flushed when a newline is printed. Unless you print a newline, or call fflush(stdout), you might not see anything at all.

    Files are buffered such that they are usually only written to when BUFSIZ characters have been written to the file. So unless you fflush() or fclose() the file, you might not see the latest data in a file either.

    In short -- if you're going to print something to the screen, either print a newline or call fflush() to make sure it gets displayed. (Or make sure more text is printed immediately afterwards, which contains a newline or whatever . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    If I remember correctly, doing
    Code:
    fprintf (stderr, "Blah");
    Will also be flushed directly to the screen without a newline, because stderr is not buffered like stdio. Hence I'd reccomend using fprintf and stderr to print out errors.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Looks like not always. http://mailman.linuxchix.org/piperma...st/000692.html

    I'm pretty sure you can make streams unbuffered, line buffered, or . . . "block" buffered at will.

    I do remember something about cerr being unbuffered and clog being line buffered in C++, even though both wrote to the standard error stream. I'm not sure what's guaranteed for C's stderr.

    It does look like stderr is usually unbuffered by default.
    The setbuf(blank) and setvbuf(blank) functions let you change the type and size of the buffers. By default, output to a terminal is line buffered, output to stderr is unbuffered, and all other I/O is fully buffered. See the setbuf(3c) man page for details.
    http://techpubs.sgi.com/library/tpl/...html/ch05.html

    I don't know how standard/unstandard this is.

    [edit] I would definitely recommend using stderr to print errors, however. perror() uses it, and the user is more likely to see any messages printed to stderr, in case they redirected stdout. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  4. Seg Fault -- Plz Help Quick
    By djwicks in forum C Programming
    Replies: 1
    Last Post: 04-10-2005, 09:08 AM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM