Like Tree1Likes
  • 1 Post By anduril462

Help: Segmentation fault (core dump) error

This is a discussion on Help: Segmentation fault (core dump) error within the C Programming forums, part of the General Programming Boards category; Hi, I am pretty new to C and having some trouble that seems pretty challenging to my limited knowledge and ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Cambridge, MA, USA
    Posts
    1

    Unhappy Help: Segmentation fault (core dump) error

    Hi, I am pretty new to C and having some trouble that seems pretty challenging to my limited knowledge and experience. Everytime I tried to execute any program containing any sort of randomization code, I get back this error message:"Segmentation fault (core dump). Here's an exemple of one of my codes

    Code:
    //This program demontrates the use of the randomization concept.
    
    #include<stdio.h>
    
    main() {
    
     int iRandomNum = 0;
     srand(time());
     iRandomNum = (rand() %4) + 1;
    
     printf("\nFortune Cookie - Chapter 3\n");
    
    switch (iRandomNum) {
     
     case 1:
     printf("\nYou will meet a new friend today.\n");
     break;
    
     case 2:
     printf("\nYou will enjoy a long and happy life.\n");
     break;
    
     case 3:
     printf("\nOpportunity knocks softly, can you hear it?\n");
     break;
    
     case 4:
     printf("\nYou'll be financially rewarded for your good deeds.\n");
     break;
     }
    }
    I have a hunch that it might be related to my system version (windows xp SP 3 / GCC 4.3.4 ) but I can't quite confirm. Please Help.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,462
    Crank up them warnings:
    Code:
    $ gcc -Wall foo.c
    foo.c:5: warning: return type defaults to ‘int’
    foo.c: In function ‘main’:
    foo.c:8: warning: implicit declaration of function ‘srand’
    foo.c:8: warning: implicit declaration of function ‘time’
    foo.c:9: warning: implicit declaration of function ‘rand’
    foo.c:31: warning: control reaches end of non-void function
    You need to include time.h and stdlib.h for the correct prototypes for srand() and time(). Then, your compiler should warn you:
    Code:
    $ gcc -Wall foo.c
    foo.c:7: warning: return type defaults to ‘int’
    foo.c: In function ‘main’:
    foo.c:10: error: too few arguments to function ‘time’
    You need to pass in a pointer to a struct tm into time, or better yet, pass NULL for the current time. Also, it's int main(void) and return an integer at the end, usually zero.

    EDIT: The reason for the seg fault was the lack of parameter to time(). The time function was grabbing whatever garbage was on the stack, which was mostly likely not a NULL address, thus trying to dereference invalid memory.
    Salem likes this.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,708
    First you need to include the include files required by srand() (stdlib.h) and time() (time.h). Also your call to time() does not have the proper number of arguments.

    You need to check your compiler settings and insure that warnings are be emitted. If you are using gcc at the command line then you should add at least -Wall -Wextra. If you are using an IDE then check the IDE's settings to turn warnings on.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String assignment segmentation fault (core dump)
    By kapil1089thekin in forum C++ Programming
    Replies: 19
    Last Post: 08-07-2010, 12:51 AM
  2. segmentation fault(core dump)
    By Pulock2009 in forum Linux Programming
    Replies: 3
    Last Post: 07-08-2010, 11:59 AM
  3. What am I doing worng? Segmentation fault, core dump
    By cookie in forum C Programming
    Replies: 4
    Last Post: 06-08-2007, 09:59 AM
  4. Very odd segmentation fault/core dump.
    By ChristianTool in forum C Programming
    Replies: 19
    Last Post: 04-26-2004, 06:38 AM
  5. Core Dump / Segmentation Fault
    By Inquirer in forum Linux Programming
    Replies: 2
    Last Post: 04-08-2003, 08:24 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