Thread: can somebody advise me on the "core dump" error

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    Segmentation fault ?!

    I have a piece of code that successfully compiles, but when I test it on my driver file, I get a "Segmentation Fault"...and nothing happens... Half of the function is ok, but then teh other half is in teh air...What am I doing wrong here...Please show me, please help
    thank you in advance...so much
    Code;
    int checker(char *file, void (*badword)(char *))
    { char *input;
    FILE *fp;
    char c;
    int i = 0;
    int result;
    char word[MAXLENGTH];
    word[0]='\0';

    if( (fp=fopen(file, "r")) == NULL)
    return SPELL_NOTFOUND;


    while( (c= getc(fp))!=EOF)
    {

    if (isalnum(c) == 0)
    { if(strlen(word) != 0)
    {
    word[i] = '\0';
    i = 0;
    if (check_word(word) == SPELL_INCORRECT)
    { if (badword != NULL)
    badword(word);
    }
    word[i] = '\0';
    }
    }
    else
    word[i++] = c;

    }
    fclose(fp);
    return SPELL_OKAY;

    }

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    can somebody advise me on the "core dump" error

    I have a piece of code that successfully compiles, but when I test it on my driver file, I get a "Segmentation Fault"...and nothing happens... Half of the function is ok, but then teh other half is in teh air...What am I am wrong here...Please show me, please help - thank you in advance...so much Code;
    int checker(char *file, void (*badword)(char *)) { char *input; FILE *fp;
    char c; int i = 0;
    int result;
    char word[MAXLENGTH];
    word[0]='\0';

    if( (fp=fopen(file, "r")) == NULL) return SPELL_NOTFOUND;

    while( (c= getc(fp))!=EOF) {

    if (isalnum(c) == 0)
    { if(strlen(word) != 0)
    {
    word[i] = '\0';
    i = 0;
    if (check_word(word) == SPELL_INCORRECT)
    { if (badword != NULL)
    badword(word);
    }
    word[i] = '\0';
    }
    }
    else
    word[i++] = c;

    }
    fclose(fp);
    return SPELL_OKAY;
    }

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    A "Segmentation Fault" occurs when you try to access a Memory Location, that has not been allottted to/by your program. A few places where this occurs is:
    1.) reading/writing to an array out of bound
    2.) manipulating FILE *, when fopen() fails
    ...

    Code:
    
    word[i++] = c;           /*   Make sure 'i' is well within word's range   */

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    1. Dont cross post
    2. Use code tags: [code&#93 /* your code */ [/code&#93

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char c;
    This should be an int since you later use it to read from getc and test for EOF. A char cannot hold the value of EOF so you need to use an int.

    As for the rest of the code, there are no glaring errors and since you didn't post something runable I can't check it more thoroughly. Perhaps if you broke your program down into something smaller and posted a complete source that we could cut, paste, and compile without adding our own test code to get it working I could help more. Because when I create my own placeholder functions it works fine.

    However, a segmentation fault is caused by a memory reference that is outside of your program's address space, usually from dereferencing a pointer with an illegal value such as NULL or uninitialized garbage or some type of corruption.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  5. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM