Segmentation Fault Core dump with C on ubuntu


I have a problem with some line of codes I've written in plain C. It worked fine on windows, but on ubuntu there's an error saying "Segmentation Fault (core dumped)". I've searched for answers but there's too many problem that can caused that error.

char line[80];
char sett[50][80];
int index=0;
static const char filename[] = "squid.conf";
FILE *file = fopen ( filename, "r" );

while ( fgets ( line, sizeof line, file ) != NULL ) 

I just simply want to write the whole file to an 2 dimensional array, line by line. If I quote the //strcpy(sett[index],line); the program runs fine with no errors.

1 Answer: 

As others have said, maybe your squid.conf have more than 50 lines, (my squid.conf have 4948 lines)

You can count lines before and malloc(nlines * 80) or you can use a linked list:

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

typedef struct sett {
    char value[80];
    struct sett *next;
} sett;

int main(void)
    char line[80];
    sett *curr, *first = NULL, *prev = NULL;
    static const char filename[] = "squid.conf";
    FILE *file = fopen(filename, "r");

    while (fgets(line, sizeof line, file) != NULL) {
        curr = malloc(sizeof(sett));
        if (curr == NULL) {
        if (prev) {
            prev->next = curr;
        } else {
            first = curr;
        strcpy(curr->value, line);
        curr->next = NULL;
        prev = curr;
    curr = first;
    while (curr) {
        printf("%s", curr->value);
        prev = curr;
        curr = curr->next;
    return 0;

More Articles

linux - Segmentation fault due to lack of memory in C

This code gives me segmentation fault about 1/2 of the time:int main(int argc, char **argv) { float test[2619560]; int i; for(i = 0; i < 2619560; i++) test[i] = 1.0f;}I actually need to allocate a much larger array, is there some way of allowing the operating system to allow me ge

linux - List out text files from given directory in unix

Writing shell script to sort text file data from given directory with for loop in ubuntu as: echo "Enter directory Name" read dr path= ` find /home/user -name $dr ` for x in ` ls *.txt $path ` do sort $x done echo "------Script finished-------"But this script is giving me er

c - Whats the difference between the two command lines? (SegFault)

I have written a code that maps to a shared memory location,so that the first program opens a shared memory block and stores some data in it.And the second program reads the shared data.Whats the difference between the two command lines:1. if(argc<2) { printf("USAGE:%s text-to-share\n",argv[0

shell - monitor for file then copy to another directory

I'm relatively new to unix scripting, so apologies for the newbie question.I need to create a script which will permanently run in the background, and monitor for a file to arrive in an FTP landing directory, then copy it to a different directory, and lastly remove the file from the original directo

Why does the following code raise a SegFault. c(Linux)

This a code that would reverse the data of a document and save it in the same document itself.However I am getting a Segmentation Fault.Please Help,I don't know why it gives a SegFault.#include <stdio.h>#include <stdlib.h>#include <termios.h>#include <fcntl.h>#include <str

linux - Docker run command error

I have just installed docker and pulled their official ubuntu image following their documentation installation tutorial.However, when I get to the point of executing a command in a specific container, I end up with the following error:Error: Cannot start container f6e1084236794548bbd5cd059f46f916367

c - Encountering segment fault when printing out directories in Ubuntu

I've been working on this code for a while and I can not figure out the problem. The idea is to print out all directories and sub directories from the given directory (hard coded for testing purposes). However, I keep getting a segmentation fault.#include <stdio.h>#include <dirent.h>#in

bash - attempting to recreate supervisor tutorial in docker, running into error (permissioning?)

I'm attempting to recreate the steps in this tutorial in a docker context, ubuntu image installed under coreos running in virtualbox on OS X.I've set up a Dockerfile that has the following steps:# Install docker basicsRUN echo "deb precise main universe" > /etc/ap

linux - Dockerfile won't run script as user with sudo rights

I have this Docker file...FROM ubuntu:14.04 MAINTAINER Sindhu S RUN apt-get update && apt-get -y install sudo wget bash git && rm -rf /var/cache/apt-get/archives/*RUN adduser --disabled-password --gecos "" scrollback RUN adduser scrollback sudo RUN ech

ubuntu - Mysql won't start anymore

I wanted to check out my logserver again, but all of sudden i get this message when i visit the url:I am running on ubuntu xenialFATAL: Cannot connect to MySQL server on 'localhost'. Please make sure you have specified a valid MySQL database name in 'include/config.php'when i do journalctl -xe i get