Thread: Segmentation Fault Error

  1. #1
    Registered User
    Join Date
    Apr 2011

    Post Segmentation Fault Error

    I have been trying to compile this program to decode a message using arrays. However I have this error that I don't know how to fix: Segmentation fault(core dumped). Does anyone know what in my code is causing this?
    #include <stdio.h>
    #define MAX 53
    FILE *mf, *cf;
    void getCode(char code[]);
    int getMessage(int msg[]);
    void sortMessage(int msg[],int msgSize);
    void decodeMessage(char code[], int msg[],int msgSize);
    int main(void)
        char code[MAX];
        int msg[MAX];
        int msgSize;
                msgSize = getMessage(msg);
                sortMessage(msg, msgSize);
                decodeMessage(code, msg, msgSize);
                return 0;
    }//end main
    void getCode(char code[]){
        fopen_s(&cf, "codefile.txt", "r");
        	fgets(code, MAX, cf);
    }//end get_code
    int getMessage(int msg[]){
    	fopen_s(&mf, "msgfile.txt", "r");
        int i;
                fscanf_s(mf,  "%d\n", &msg[i++]);
          return i;
    }//end get_msg
    void sortMessage(int msg[],int msgSize){
          //Sort message
          int j, i, temp;
                for(i = 1; i < msgSize; i++)
                      temp = msg[i];
                      j = i - 1;
                      while(j >= 0 && temp < msg[j]){
                            msg[j+1] = msg[j];
                            j = j - 1;
                      }//end while
           msg[j+1] = temp; //reset the contents into msg.
            }//end for loop
    }//end sort_msg
    void decodeMessage(char code[], int msg[],int msgSize){
          //Prints decoded message.
    	int i;
    	for(i = 0; i < msgSize; i++)
                printf("%c", code[msg[i] % 100]);
    }//end decode_msg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > int i;
    This is uninitialised, so when you use it to access an array, who knows what happens.

    > while(!feof(mf))
    See the FAQ for why using feof() to control a loop is bad.

    > fscanf_s(mf, "%d\n", &msg[i++]);
    You should avoid using the Microsoft-specific _s (supposedly "safe") versions of these functions.
    They won't protect you from doing stupid things if you don't know how to use them to begin with, and they just lock you into their compiler.
    If you're worried by some "deprecated" warning, we can tell you how to avoid that.

    > return i;

    > fclose(mf);
    But you don't close the file, since you already returned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Thank you sooo much! I just fixed what you told me and it works. I can't believe I was stuck on this for so long.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC Segmentation fault error.
    By JPinUSMC in forum C Programming
    Replies: 2
    Last Post: 02-21-2011, 10:59 PM
  2. Segmentation Fault Error
    By c3jcarmy in forum C Programming
    Replies: 4
    Last Post: 01-25-2011, 09:48 AM
  3. Segmentation fault Error
    By unknown_ in forum C Programming
    Replies: 7
    Last Post: 03-21-2010, 01:32 PM
  4. Segmentation fault error
    By ashok449 in forum C Programming
    Replies: 31
    Last Post: 03-02-2008, 02:13 AM
  5. Segmentation Fault Error
    By jcramer in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 02:16 PM

Tags for this Thread