Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes a segmentation fault (core dump) to occur in C?

I am trying to write a Hamming code program in C. However, I keep getting a Segmentation Fault(Core Dumped) error when trying to run the ./a.out after compiling. It compiles with no errors, and I understand that this error can occur when trying to address freed space or modifying a string literal. I don't believe I am doing either of those things, I just have a simple matrix I am filling and cross checking. Any insight on the issue would be appreciated, I have left what code I have so far below:

This is for a homework problem involving creating a Hamming code program to handle data.dat input and output to a file a sorted list of 1's and 0's

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

FILE *fp;
FILE *fpOut;

int main(int argc, char *argv[])
{
        fp = fopen(argv[1], "r");
        fpOut = fopen("sortedCodeWords.dat", "w");

        int temp;
        int numI = 0;
        int count = 0;

        char matrix1[7];

        if(fp == NULL)
                printf("File can not be opened");
        else
        {
                char ch = getc(fp);

                while(ch != EOF)
                {

                        matrix1[2] = ch;
                        ch = getc(fp);

                        matrix1[4] = ch;
                        ch = getc(fp);

                        matrix1[5] = ch;
                        ch = getc(fp);

                        matrix1[6] = ch;
                        ch = getc(fp);

                        ch = getc(fp);

                        if(ch == '\n')
                        {
                                for(int i = 2; i < 7; i++)
                                {
                                        if(matrix1[i] == '1')
                                            numI++;

                                        i++;
                                }

                                if(numI % 2 == 0)
                                        matrix1[0] = 0;
                                else
                                        matrix1[0] = 1;

                                numI = 0;

                                for(int i = 1; i < 7; i++)
                                {
                                        if(matrix1[i] == '1')
                                                numI++;
                                        if(matrix1[i+1] == '1')
                                                numI++;

                                        i++;
                                        i++;
                                }

                                if(numI % 2 == 0)
                                        matrix1[1] = 0;
                                else
                                        matrix1[1] = 1;

                                numI = 0;

                                for(int i = 4; i < 7; i++)
                                {
                                        if(matrix1[i] == '1')
                                                numI++;
                                }

                                if(numI % 2 == 0)
                                        matrix1[3] = 0;
                                else
                                        matrix1[3] = 1;
                                numI = 0;

                                for (int i = 0; i < 7; i++)
                                {
                                        fprintf(fpOut, "%s", matrix1[i]);
                                }

                                fprintf(fpOut, "\n");

                                ch = getc(fp);
                        }

                        count++;
                }
        }
}

I expect an output to a file. I didn't always get this error, but when I changed from a 2D array to 1D array, I am now getting this error (I changed because I realized it wasn't necessary)

like image 628
V.Bean Avatar asked Jan 01 '23 02:01

V.Bean


2 Answers

I’ve not compiled it, but the one thing I notice is that it looks as if you’re potentially going off the end of your array where you’re looping to i<7 but using an index of i+1 in one instance.

Maybe build with AddressSanitizer enabled and see what runtime warnings you then get once you’ve checked the potential issue above. (It should just be a matter of adding the following flags to you gcc command... -g -fsanitize=address -fno-omit-frame-pointer

like image 123
Gwyn Evans Avatar answered Jan 05 '23 18:01

Gwyn Evans


Two things I see. First, you're mixing chars with ints in the matrix array. Second, you're printing the elements of matrix out to a file using the "%s" format. "%s" expects a null-terminated string where as you are passing chars and ints. This will cause the printf to try and access memory that is out-of-bounds, thus the fault.

like image 31
sizzzzlerz Avatar answered Jan 05 '23 16:01

sizzzzlerz