Reputation: 1170
I'm working on a List example in C where new nodes are pushed on to the end of the stack. I keep getting a Bus Error: 10
when I try to push the new node on to the end. Here is my push function:
void push(struct node *tail, struct node *newNode) {
tail->next = newNode; // gdb says the problem is here
tail = tail->next;
}
and I call it using push(tail, newNode);
Also here is my struct if necessary:
struct node
{
int hour;
int minute;
char *name;
struct node *next;
};
And here is the main function showing code leading to push()
int main()
{
char inputString[50];
int timeHour, timeMin;
struct node *head;
struct node *tail;
while ((scanf("%d:%d", &timeHour, &timeMin)) != EOF) {
scanf("%s", inputString);
if (strcmp(inputString, "enqueue") == 0) {
if (head == NULL) {
head = malloc(sizeof(struct node));
head->hour = timeHour;
head->minute = timeMin;
// get name
scanf("%s", inputString);
head->name = malloc(strlen(inputString)+1);
strcpy(head->name, inputString);
tail = head;
printEnqueue(head);
} else {
struct node *newEntry = malloc(sizeof(struct node));
newEntry->hour = timeHour;
newEntry->minute = timeMin;
// get name
scanf("%s", inputString);
newEntry->name = malloc(strlen(inputString)+1);
strcpy(newEntry->name, inputString);
push(tail, newEntry);
printEnqueue(newEntry);
}
} else {
pop(&head, timeHour, timeMin);
}
}
return 0;
}
Upvotes: 1
Views: 1201
Reputation: 13386
I suspect the head
and tail
node in the main
function is not properly initialized.
From your code it seems that head
will be allocated a new node if it is NULL
. However, your definition of head
does not ensure it's initially NULL
(neither does tail
). So you may bypass the if (head == NULL)
branch (make sure they are really executed from gdb
please :) ).
The Bus error
is rarely seen. So I googled it, and from here, a bus error may occur when
using a processor instruction with an address that does not satisfy its alignment requirements.
This may be because tail
is not aligned and code runs directly into else
branch. So
push(tail, newEntry);
will access tail which is not aligned (this also validates my suspect).
Upvotes: 2
Reputation: 36102
change
void push(struct node *tail, struct node *newNode)
{
tail->next = newNode; // gdb says the problem is here
tail = tail->next;
}
to
void push(struct node **tail, struct node *newNode)
{
(*tail)->next = newNode; // gdb says the problem is here
(*tail) = (*tail)->next;
}
then call it like this instead
push(&tail, newEntry);
as you have it currently 'tail' will never change since you do not pass the address of the variable to the function so you cannot change what it points to.
also make sure you initialize all your local variables (header,tail,...), make it a habit.
Upvotes: 1
Reputation: 15642
Amendment #3: while ((scanf("%d:%d", &timeHour, &timeMin)) != EOF)
Within the body of this loop, there is no guarantee that the two integers timeHour
and timeMin
were assigned to. Perhaps you meant while ((scanf("%d:%d", &timeHour, &timeMin)) == 2)
.
Amendment #2: When you pass a value to a function, you're passing the value, not the variable. The assignment you're making to tail
within push
isn't visible to the caller (your main
). You'll need to pass in a pointer to that variable (eg. &head
which is a struct node **
) and assign to *tail
as you were, previously. Alternatively, you could return newNode;
from your push
and use the return value as your new head
.
Amendment: This doesn't even look like it'll compile. Let's take a look at push
.
void push(struct node **tail, struct node *newNode) {
(*tail)->next = *newNode; // gdb says the problem is here
*tail = (*tail)->next;
}
What's the type of *newNode
? struct node
.
What's the type of (*tail)->next
? That's in this snippet:
struct node
{
int hour;
int minute;
char *name;
struct node *next;
};
Fix your inconsistency and ensure your minimal, compilable testcase is compilable before you post it.
Don't forget to check the return value of scanf
! In your case, it should return 1 unless an error occurs.
head->name = malloc(strlen(inputString));
strcpy(head->name, inputString);
This is wrong, because you're not allocating enough space to store a '\0'
character. I think you meant malloc(strlen(inputString) + 1)
. There are two instances of this error in your code. I'm not planning on repeating myself.
struct node *newEntry = malloc(sizeof(struct node));
push(&tail, newEntry);
What's the type of newEntry
? struct node *
.
void push(struct node **tail, struct node **newNode)
What's the type of newNode
? struct node **
. Do you see the inconsistency? You need to pass in a struct node **
, but newEntry
is a struct node *
.
Upvotes: 1