#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    42
    Rep Power
    12

    Segmentation Fault


    Hey, I've just gotten a fatal error in a program I'm writing and have no idea why. What I'm writing is a very basic AI that associates words that I input with ones that I say following that. I have an array that contains words that it knows already, and naturally at the beginning of the program this would be empty. So when I type in hello, it gives me a segmentation fault (at least in linux. In windows it just tells me the program has had an error and can't continue.) First off, I have no idea what a segmentation fault is, and I'm pretty sure that it's not just a compiler error (although technically I've only tested it on one compiler... I use dev-c++ in windows and just the g++ compiler in linux. dev I'm pretty sure uses the djgpp port.) So if anyone has any previous knowledge of what might cause this, I'm open to suggestions, and if anyone wishes to see the code I'd be happy to post it. Thanks,
    Eric
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    42
    Rep Power
    12
    Okay, after looking around at some other segmentation posts, I've realized that it's probably my two arrays that I use to store strings in. I have one called history and another called associations:

    char *history[64];
    char *associations[128];

    After looking around, I figured that maybe I needed to try malloc()ing them before I use them, so I tried this:

    char *history[64] = (char **)malloc(64 * sizeof(char));

    which doesn't come up as a valid declaration in my compiler. Any ideas?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    42
    Rep Power
    12
    Well, I seem to have figured it out without using malloc. For those of you who are interested, what I was doing was going through a for loop and seeing if a string matched any of the string in the another array. My problem came in that I was letting it search throught he undeclared elements of the array.
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    char *history[64] is typed as a pointer to a 64 element character array, so your compiler is rejecting that declaration. also, when you malloc(64 * sizeof(char)) ; you are only allocating 64 bytes of memory. this would leave you with 1 byte for each of your strings, which i dont think you wanted. try something like this:

    Code:
    char *history[64];
            int x = 0;
            
            for(x; x < 5; x++)
                    history[x] = (char *)malloc(24 * sizeof(char));
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Sorry noone responded to your problem before you figured it out, but I'm glad that you figured it out.

    To highlight on a couple of things for you though:
    A segmentation fault is a *nix only error, and it is basically the same as the error you got in windows. It is a fatal error in which the program cannot continue running.

    Seeing as you were trying to allocate memory for an array of strings, there are a few options that you could do here that might benefit you better.
    Test to see if the element is NULL (fyi: performing most any operation with a string function with NULL will cause a segmentation fault, i.e. strcpy(), etc) and if it is, then malloc the data for that string.
    history[element_number] = (char *)malloc(max_size_of_string * sizeof(char));

    or you can create a loop to allocate memory for each string at one time.
    i.e.
    Code:
    #define MAX 256
    int i=0;
    char *history[64];
    
    for (i=0; i<64; i++) {
        history[i] = (char *)malloc(MAX * sizeof(char));
        if (NULL == history[i]) {
            fprintf(stderr, "Unable to allocate memory for history.");
            exit(1);
        }
    }

    *edit* I see infamous beat me too it. ;)
  10. #6
  11. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    copycat :D

    edit: hehe just saw your edit!
  12. #7
  13. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    great minds think alike :D

IMN logo majestic logo threadwatch logo seochat tools logo