NO

Author Topic: I must be doing something wrong  (Read 1289 times)

Offline Panos

  • Member
  • *
  • Posts: 2
I must be doing something wrong
« on: May 02, 2021, 09:45:55 AM »
I don't understand why the following code results in error unhandled exception:

enqueue 50
enqueue 100
dequeue all to rear = 100---
ok2
50
CRT: unhandled exception (main) -- terminating
*** Process returned 255 ***


The problem seems to be in the line if (q -> front == NULL) {, but if I remove the comment from the test message above that line, it works. This probably means that I'm doing an undefined behavior of sorts, but I really don't understand why. The code works fine in GCC, but I don't think there's something wrong in Pelle's, there must be something wrong with my code, I just can't figure it out.

Can anyone help ?

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

typedef struct test1 {
int index;
struct test1 *next;
} test1;

typedef struct {
test1 *front;
test1 *rear;
} test2;

test1 dequeue(test2 *q)
{
test1 dequeued = {-1, NULL};
if (q -> front != NULL) {
memcpy(&dequeued, q -> front, sizeof dequeued);
test1 *p = q -> front;
q -> front = q -> front -> next;
//puts("ok1");
if (q -> front == NULL) {
puts("ok2");
q -> rear = NULL;
}
free(p);
}
return dequeued;
}

void enqueue(test1 pr, test2 *q)
{
printf("enqueue %d\n", pr.index);

test1 *newnode = malloc(sizeof pr);
assert(newnode != NULL);
memcpy(newnode, &pr, sizeof pr);
newnode -> next = NULL;

if (q -> front == NULL) {
q -> front = newnode;
} else {
q -> rear -> next = newnode;
}
q -> rear = newnode;
}

int main()
{
test2 Q = {NULL};
test1 pr;

pr.index = 50;
enqueue(pr, &Q);
pr.index = 100;
enqueue(pr, &Q);

printf("dequeue all to rear = %d---\n", Q.rear -> index);
while (Q.front != NULL) {
pr = dequeue(&Q);
printf("%d\n", pr.index);
}
}
« Last Edit: May 02, 2021, 09:52:12 AM by Panos »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: I must be doing something wrong
« Reply #1 on: May 02, 2021, 12:39:08 PM »
I think you are fine... the compiler... not so much.

It changes (propagates):
Code: [Select]
if (q -> front == NULL) {into something like:
Code: [Select]
if (q -> front -> next == NULL) {which is obviously wrong.

In this little test, changing the line from:
Code: [Select]
if (q -> front == NULL) {to:
Code: [Select]
if (p == NULL) {seems to work. Not sure how much of a general solution this is.

Apparently the compilers "alias oracle" went out for coffee around this line, or something. Probably something. It's been completely rewritten in the version I'm working on, and this test case works fine with it, but this is of course of little help to you at the moment. I'm aiming for a new release later this year, we'll see if the world agrees with that...

/Pelle

Grincheux

  • Guest
Re: I must be doing something wrong
« Reply #2 on: May 02, 2021, 01:08:56 PM »
Don't use GG, here there is the best C compiler and it is FREE

Offline Panos

  • Member
  • *
  • Posts: 2
Re: I must be doing something wrong
« Reply #3 on: May 02, 2021, 02:45:12 PM »
Thank you Pelle for letting me know. Yes, your tip fixes the problem, and it will suffice for now. Great news about the rewrite!  :)

Don't worry Grincheux, I'm not considering to stop using Pelles C, it's just too neat to ignore.