Like Tree1Likes
  • 1 Post By MK27

Segmentation Fault

This is a discussion on Segmentation Fault within the C Programming forums, part of the General Programming Boards category; Hello everyone, I've been programming in Java for approx 1.5 years and am moving to C now for my CS ...

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

    Segmentation Fault

    Hello everyone,

    I've been programming in Java for approx 1.5 years and am moving to C now for my CS degree. I have a book that I'm moving through pretty fast but am kind of stuck on a rand number problem which keeps giving me a segmentation fault error. Right now I'm stuck because this is an example in the book which is compiling and linking but not executing. I'm on Ubuntu 11.10 if it matters I think the book was designed for windows. Here is the block of code.

    Code:
    #include<stdio.h>
    
    main(){
       int iRandNum = 0;
       srand(time());
       iRandNum = (rand() % 4) + 1;
       printf("%d", iRandNum);
    }
    I've been googling all day and the only thing I can come up with is that it's an OS specific problem because it has something to do with a hardware error. I don't think that's what it is but that's what I've been seeing. Thanks a lot guys

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    First,
    Code:
    #include <stdlib.h>  // for rand()
    #include <time.h>   // for time()
    Tho because they both return the default int, everything will work out anyway. Which BTW, main() should also return an int.

    Next, that code should not even compile -- time() requires an argument. What compiler are you using?
    Salem likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by MK27 View Post
    First,

    Next, that code should not even compile -- time() requires an argument. What compiler are you using?
    thats probably why it is crashing. time wants a pointer and it is getting garbage. it compiles because it doesn't have the prototypes for time and rand so it defaults.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    I am using gcc.

    I've changed my code to look like such

    Code:
    #include<stdio.h>
    #include<time.h>    // added
    #include<stdlib.h>   // added
    
    main(){
       int one = 0;
       srand(time());
       one = (rand() % 4)+1;
       printf("%d", one);
       return 0;             // added
    }
    after such I'm now getting errors compiling.

    error: too few arguments to function 'time'
    /usr/include/time.h:186:15 note: declared here



    Ok, I fixed/figured it out. Thank you dmh and mk27. I placed "NULL" into the time function and like magic it works thanks guys!!!!
    Can I some how mark this as solved or close it?
    Last edited by seanfmglobal; 12-22-2011 at 10:37 AM. Reason: fixed

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by seanfmglobal View Post
    Can I some how mark this as solved or close it?
    Nah, no need. Maybe someone will still add more ideas... but threads usually just stay open forever. Less work for the moderators that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault!!
    By Necromancer in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 07:03 AM
  2. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  3. Segmentation Fault
    By warfang in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2007, 01:42 AM
  4. Segmentation Fault
    By Ajay Bahadur in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2007, 11:27 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

Tags for this Thread


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