Trying to Understand this Segmentation Fault?

Tail is NULL and you're dereferencing it on the line you marked. It needs to be made to point to a Event before you can dereference it.

Up vote 1 down vote favorite share g+ share fb share tw.

I am trying to understand why I am getting a segfault at the indicated line of my code below (noted with: nextEvent = &newEvent; newEvent. NextEvent = NULL; } else { // first element being added to queue. *tail = &newEvent; // 3) { printf("WARNING: input format error!

\n"); break; } if (i == 0) strncpy(event. Action, lineSegment, sizeof(event. Action)-1); else if(i == 1) event.

Timestamp = atoi(lineSegment); else if(i == 2) { event. Path = malloc(sizeof(char) * (strlen(lineSegment) + 1)); strcpy(event. Path, lineSegment); } else if(i == 3) strncpy(event.

Hash, lineSegment, sizeof(event. Hash)-1); lineSegment = strtok(NULL, " "); i++; } // while return event; } // parseLineIntoEvent() int main (int argc, const char * argv) { //... Event **head = NULL; Event **tail = NULL; for (; numLines > 0; numLines--) { char *line = getLineFromStdin(); //malloced char array being returned printf("%s\n",line); Event event = parseLineIntoEvent(line); if(!enqueue(event, head, tail)) printf("An error occurred when attempting to enqueue an Event. \n"); event = dequeue(head, tail); //... free(event.

Path); free(line); } return 0; } Thanks in advance! C memory-management queue segmentation-fault parameter-passing link|improve this question asked Jan 17 at 23:35Stunner620421 76% accept rate.

The enqueue function is broken. You pass the newEvent by value, this will not modify the event you pass in. – pmr Jan 17 at 23:39.

Event **tail = NULL; tail is NULL, and you're dereferencing it on the line you marked. It needs to be made to point to a Event* before you can dereference it: Either Event *ev; Event **tail = &ev; Or Event** tail = malloc(sizeof(Event*)); ... free(tail); Though, I think you mean to have just a pointer to an Event, and pass its address by value: Event *tail = NULL, *head = NULL; ... enqueue(event, &head, &tail); So that head and tail are modified inside enqueue.

1 The question is tagged C; so new is inappropriate. – Dave Jan 17 at 23:44 @Dave sorry, I forget which tag I'm in all the time. For such stupid errors, please feel free to edit my answers in the future.

– Seth Carnegie Jan 18 at 0:25.

You are dereferencing a null pointer. Int main (int argc, const char * argv) { //... Event **head = NULL; Event **tail = NULL; for (; numLines > 0; numLines--) { //... if(!enqueue(event, head, tail)) // head and tail are NULL here //... } } int enqueue(Event newEvent, Event **head, Event **tail) { if (head! = NULL) { // head is NULL, else path will be executed //... } else { // first element being added to queue.

*tail = &newEvent.

Your code contains at least two bugs: As pointed out by others you are passing in a NULL pointer for tail and dereferencing will cause the segfault You pass in newEvent by value. This means a copy of the struct will be made and put on stack. Inside the function you do *head = &newEvent which takes the address of the object which is on the stack and put it in the queue.

After you return from the enqueue function the stack will be cleaned up and you point to garbage. You need to change your eneueue to int enqueue(Event *newEvent, Event **head, Event **tail) and pass a pointer to the event. I'm not sure why you use pointer to pointer and also your queue seems semantically broken (head is typically the start and enqueue typically appends things at the end) int enqueue(Event *event, Event **head, Event **tail) { if (*head!

= NULL) { // we do have a head: point next to current head event->nextEvent = *head; } else { // no head so we also should have no tail *tail = event; event->nextEvent = NULL; } // newly enqueued event is now head *head = event; return 1; } Use it: Event *head = NULL; Event *tail = NULL; ... Event newEvent = parseLineIntoEvent(char* line); enqueue(&newEvent, &head, &tail); Although you should consider putting newEvent on the heap by returning Event * from parseLineIntoEvent (use malloc to allocate the memory for it).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions