I am just playing around with a queue data type in C. I have a very basic program that inserts 300 't' chars into the queue and spits em back out. First here are the relevant queue functions :
Here is the main routine that doesn't work :
This gives me a BUS ERROR (core dumped) message when running the program
The following runs just fine... the only difference is that I allocated memory for the temp character pointer
The following also works, but look at what has changed... instead of passing a char to the qinsert function I hardcoded
't' in the argument list! I didn't allocate memory for the temp character pointer and it runs fine!
Can any C gurus shed a little light on this? I am glad I got it working how I wanted it to, but I don't get why it works!
If you need more code just ask. Yes this is for an assignment, but what I am asking is not in the scope of the assignment
so by explaining this to me you aren't helping me cheat in any way... thanks
			
			struct qentry
{
struct qentry *next;
char d;
};
struct queue
{
struct qentry *begin, **end;
int size;
};
int qinsert(struct queue *q, char d)
{
if (!q || !(*q->end = (qentry*)malloc(sizeof(struct qentry)))) return 0;
(*q->end)->d = d;
(*q->end)->next = NULL;
q->end = &((*q->end)->next);
q->size++;
return 1;
}
char * qremove(struct queue *q, char *d)
{
struct qentry *tmp;
if (!q || !q->begin) return NULL;
tmp = q->begin;
if (!(q->begin = q->begin->next)) q->end = &q->begin;
*d = tmp->d;
free(tmp);
q->size--;
return d;
}
Here is the main routine that doesn't work :
int main (int argc, char **argv) {
queue* inq;
inq = qinit(NULL);
int i;
char blah = 't';
for (i=0;i<300;i++) {
qinsert(inq,blah);
}
char * temp;
for (i=0;i<300;i++) {
qremove(inq,temp);
printf("%c\n", *temp);
}
return 0;
} /* main */
This gives me a BUS ERROR (core dumped) message when running the program
The following runs just fine... the only difference is that I allocated memory for the temp character pointer
int main (int argc, char **argv) {
queue* inq;
inq = qinit(NULL);
int i;
char blah = 't';
for (i=0;i<300;i++) {
qinsert(inq,blah);
}
char * temp = (char*)malloc(sizeof(char));
for (i=0;i<300;i++) {
qremove(inq,temp);
printf("%c\n", *temp);
}
return 0;
} /* main */
The following also works, but look at what has changed... instead of passing a char to the qinsert function I hardcoded
't' in the argument list! I didn't allocate memory for the temp character pointer and it runs fine!
int main (int argc, char **argv) {
queue* inq;
inq = qinit(NULL);
int i;
for (i=0;i<300;i++) {
qinsert(inq,'t');
}
char * temp;
for (i=0;i<300;i++) {
qremove(inq,temp);
printf("%c\n", *temp);
}
return 0;
} /* main */
Can any C gurus shed a little light on this? I am glad I got it working how I wanted it to, but I don't get why it works!
If you need more code just ask. Yes this is for an assignment, but what I am asking is not in the scope of the assignment
so by explaining this to me you aren't helping me cheat in any way... thanks
 
				
		 
			 
 
		 
 
		 
 
		 
 
		 
 
		
 Facebook
Facebook Twitter
Twitter