Thread: segmentation fault(coredump)

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    19

    segmentation fault(coredump)

    I think that I have the definition of segmentation fault covered and why other people are receiving this error when I checked on past posts. However, I have no idea why Im getting it....can someone help! Thanks in advance!!!

    Code:
    program7.c
    
    #include "program7.h"
    
    int main(int argc, char *argv[])
    {
      char **StringList;
      int i, num, length;
    
      if (argc != 3)
        printf("Insufficient arguments\n");
    
      num = atoi(argv[1]);
      length = atoi(argv[2]);
    
      StringList = (char **)calloc(num, sizeof(char *));
    
      srand(time(NULL));
    
      for (i = 0; i < num; i++){
          StringList[i] = RandomString(length);
      }
      printf("Unsorted array:%s\n", StringList[i]);
    
      /*StringSort(num, StringList)*/
      /*printf("Sorted array:%s\n", StringList[i];
      /*4*/
    
      return 0;
    }
    
    ****************************************************
    
    RandomString.c
    
    #include "program7.h"
    
    char *RandomString(int length){
    
      int i, size;
      char *string;
    
      size = 1 + rand() % length;
    
      string = (char *)calloc(size + 1, sizeof(char));
    
      for(i = 0; i < size; i++){
        string[size] = (char)((rand() % 26) + 65);
      }
      string[size] = '\0';
    
      return string;
    
    } /* End of RandomString function */

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    which compiler are you using?

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    Im using the cc compiler on UNIX system.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Print from inside the loop, not after i is beyond memory you own.
    Code:
      for (i = 0; i < num; i++){
          StringList[i] = RandomString(length);
          printf("Unsorted array:%s\n", StringList[i]);
      }
    Use your loop index instead of writing everything to the last character and then erasing it.
    Code:
      for(i = 0; i < size; i++){
        string[i] = (char)((rand() % 26) + 'A');
      }
      string[i] = '\0';
    Last edited by Dave_Sinkula; 11-19-2003 at 08:04 PM.
    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.*

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    Holy $h--, it worked....Thanks a lot Dave, I've been working on that for a long, long time!!!

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    What if I wanted to put each of those strings in dictionary order?
    i.e.
    string[1] = dog
    string[2] = dogo
    string[3] = efgjoz
    string[4] = ijs
    ...
    The below code doesnt work and I have been trying to modify it, but no such luck! Any more suggestions?

    Code:
    #include "program7.h"
    
    void StringSort(int num, char *StringList){
    
      int i, j, temp;
    
      for(i = 0; i < num; ++i){
        for(j = num; j >= i; --j){
          if(StringList[j - 1] < StringList[j])
            temp = StringList[j - 1];
          StringList[j - 1] = StringList[j];
          StringList[j] = temp;
    
        } /* End of 1st for statement */
    
      } /* End of 2nd for statement */
    
    } /* End of StringSort function */

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: segmentation fault(coredump)

    Originally posted by chauncey005
    Code:
      if (argc != 3)
        printf("Insufficient arguments\n");
    If you are testing for "Insufficient arguments", why do you continue merrily on your way and process the code anyway? You should test for the error and exit, shouldn't you?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    Good point, Walt! Thanks for showing me that...I wouldnt have even noticed it!

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    An adaptation of this:
    Code:
    void StringSort(int num, char **StringList)
    {
       int i, j;
       for ( i = (num - 1); i >= 0; i-- )
       {
          for ( j = 1; j <= i; j++ )
          {
             if ( strcmp(StringList[j - 1], StringList[j]) > 0 )
             {
                /* 'previous' is bigger than 'next' -- swap pointers */
                char *temp        = StringList[j - 1];
                StringList[j - 1] = StringList[j];
                StringList[j]     = temp;
             }
          }
       }
    }
    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.*

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    Thank you, I can generate the random strings and I tried the code for the StringSort below, however, when I try to sort them I am now receiving the error: segmentation fault(coredump). Ive tried resituating the code as was the case before, but that doesnt work. Any more ideas anyone? Thank you again!

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The following was my "putting it all together" test code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    char *myrandstr(int length)
    {
       int i, size = 1 + rand() % length;
       char *word = malloc((size + 1) * sizeof *word);
       if ( word )
       {
          for ( i = 0; i < size; i++ )
          {
             word[i] = (char)((rand() % 26) + 'A');
          }
          word[i] = '\0';
       }
       return word;
    }
    
    /* http://linux.wku.edu/~lamonml/algor/sort/bubble.html */
    void mystrsort(int num, char **word)
    {
       int i, j;
       for ( i = (num - 1); i >= 0; i-- )
       {
          for ( j = 1; j <= i; j++ )
          {
             if ( strcmp(word[j - 1], word[j]) > 0 )
             {
                char *temp  = word[j - 1];
                word[j - 1] = word[j];
                word[j]     = temp;
             }
          }
       }
    }
    
    int main(int argc, char *argv[])
    {
       if ( argc == 3 )
       {
          int i, num = atoi(argv[1]), length = atoi(argv[2]);
          char **word = malloc(num * sizeof *word);
          if ( word )
          {
             srand(time(NULL));
             /*
              * Make a list and print it.
              */
             puts("---Unsorted---");
             for ( i = 0; i < num; i++ )
             {
                word[i] = myrandstr(length);
                puts(word[i]);
             }
             /*
              * Sort it and print it.
              */
             mystrsort(num, word);
             puts("----Sorted----");
             for ( i = 0; i < num; i++ )
             {
                puts(word[i]);
             }
             /*
              * Clean up.
              */
             for ( i = 0; i < num; i++ )
             {
                free(word[i]);
             }
             free(word);
          }
       }
       return 0;
    }
    
    /* my output
    C:\test>test 5 10
    ---Unsorted---
    KXMFKFPRXT
    LQEPET
    S
    IIIAPLFWR
    MAJZMF
    ----Sorted----
    IIIAPLFWR
    KXMFKFPRXT
    LQEPET
    MAJZMF
    S
    */
    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.*

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    That works, but unfortunately we cant use strcmp() or puts()...but thanks anyways!

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but unfortunately we cant use strcmp() or puts()...
    strcmp is trivial to write, and puts functionality can be simulated with printf (not to mention that puts was used to test that the function worked so that you would get correct code). You should be a little more considerate when someone posts a largish chunk of code that was written just for your benefit.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM