Question

I have a C project, and I'm quite new with C and the linux environment.

I'm working on a linux distribution with the following system info

Linux bt 3.2.6 #1 SMP Fri Feb 17 10:34:20 EST 2012 x86_64 GNU/Linux

After compiling with gcc, on the said OS, I got the waited results.

Before submitting my project to the professor, I thought of trying to compile and execute the program on another linux distribution with the following system information

Linux feistyFawn 2.6.20-15-generic #2 SMP Sun Apr 15 07:36:31 UTC 2007 i686 GNU/Linux

And I get segmentation fault under this one. To illustrate the output console. Here are the images.

Works

enter image description here

Fails

enter image description here

I'm not sure what to do now.


Code

A call to this function causes segmentation fault on another OS.

fileInEvenements(listEvents, 'A', time, queueId);

What it does is file in a event called A into a queue structure, listEvents.

And its definition

void fileInEvenements(eventStructure *listEvents, char eventType, int time_exec, int id_queue)
{
    Event *newEvent = malloc(sizeof(*newEvent));
    if (newEvent == NULL || listEvents == NULL){
        exit(EXIT_FAILURE);
    }

    newEvent->type = eventType;
    newEvent->execution_time = time_exec;
    newEvent->id = id_queue;
    if (listEvents->firstEvent != NULL)
    {
        // the list contains at least one event, go to the end of list 
        Event *evCurrent =  listEvents->firstEvent;
        while (evCurrent->next != NULL)
        {
            evCurrent = evCurrent->next;
        }
        evCurrent->next = newEvent;
    }
    else // the list contains no event, new event becomes first event
    {
        listEvents->firstEvent = newEvent;
    }

}
Was it helpful?

Solution

When you are creating a new entry in your linked list, you are attempting to append it to the list by starting at the head and iterating the list until you find a NULL in evCurrent->next. When you do find a NULL you stop iterating the list and, at that point, assign newEvent as the next entry in the list via evCurrent->next = newEvent; — and, of course, if there are no entries in the linked list, you make the new entry the head of the list via listEvents->firstEvent = newEvent;

However, at no time do you initialize the value of newEvent->next. Note that malloc() does not initialize the block of memory it returns. It simply allocates a block and returns it to you. See the docs here http://www.cplusplus.com/reference/cstdlib/malloc/

The key bit is this...

The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.

Therefore, newEvent->next is, for all practical purposes, a random value. Thus, your code is off into random memory walks because you are counting on it being NULL to terminate the linked list.

I might suggest you try calloc() instead.

Evenement *newEvent = calloc( 1, sizeof(*nvEvent) );

Otherwise, ensure that the value of the next element is initialized to NULL when you create it.

OTHER TIPS

Add a line:

newEvent->next = NULL;

Using an unitialized field is an error.

Alternatively you can use on the following:

newEvent = calloc(sizeof(*newEvent), 1); // instead of malloc

Or

memset(newEvent, 0, sizeof(*newEvent)); // set all fields to 0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top