0

I am using a priority queue with a double as the priority. I am guessing this is the cause of the issues. I used these numbers first with no issues.

34.365681 34.481879 34.539832 36.715120

I then used these numbers and had a segmentation fault.

45.411042 40.481879 37.702110 38.951187

struct PRIORITYQUEUE
    {
    int x_pq; 
    int y_pq;
    double heuristic_pq;
    int priority;
    int info;
    struct PRIORITYQUEUE *next;
}*start, *q, *temp, *new;

typedef struct PRIORITYQUEUE *N;

void insert(int x, int y, double heuristic)
{

    int item; 
    double itprio;
    //new = ( N* ) malloc( sizeof( N ) );
    new = malloc( sizeof( N ) );

    itprio = heuristic;
    new->x_pq = x;
    new->y_pq = y;
    new->heuristic_pq = itprio;

    if ( start == NULL || itprio < start->heuristic_pq )
    {
        new->next = start;
        start = new;
    }
    else
        {
            q = start;
            while ( q->next != NULL && q->next->heuristic_pq <= itprio )
                q = q->next;

            new->next = q->next;
            q->next = new;
        }
}

void del()
{
    if ( start == NULL )
    {
         printf( "\nQUEUE UNDERFLOW\n" );
    }
    else
    {
    new = start;
    printf( "\nDELETED ITEM IS %d\n", new->info );
    start = start->next;
    free( start );

    }

}

void display()
{
    temp = start;
    if ( start == NULL )
        printf( "QUEUE IS EMPTY\n" );

    else
    {
        printf( "QUEUE IS:\n" );

        while ( temp != NULL )
        {
            printf( "\t x is %d y is %d[heuristic=%lf] \n", temp->x_pq, temp->y_pq, temp->heuristic_pq );
            temp = temp->next;
        }
     }
}
1
  • 4
    Likely want new = malloc(sizeof *new); sizeof( N ) is the size of the pointer, not the size of the structure. Commented Oct 19, 2014 at 2:49

1 Answer 1

1

Your problem lies with this code:

typedef struct PRIORITYQUEUE *N;
:
new = malloc( sizeof( N ) );

The type N is a pointer to that structure of yours, not the structure itself. That means that sizeof(N) is likely to be much smaller than the structure, meaning that you're not allocating enough memory.

You could see this by inserting this immediately after the allocation:

printf ("%zd %zd\n", sizeof (N), sizeof (struct PRIORITYQUEUE));

and you'll probably see a series of lines of the form 4 32 or 8 32, showing that, while you've allocated four or eight bytes, you need 32.

That's what's causing your crashes. Now, as to how to fix it, it's simply making sure you allocate enough space for the structure and this can be done with either of:

new = malloc (sizeof (struct PRIORITYQUEUE));
new = malloc (sizeof (*N));

But the one I prefer is:

new = malloc (sizeof (*new));

The reason I prefer it is that it ties the allocation quantity to the variable you using. While the earlier two will handle any changes to the structure size, this one will even survive declaring new as a totally different structure without having to change information in multiple places.

By that I mean, if you change the type of new thus:

struct FASTER_PRIO_Q *new;

then you would be required to change the allocation statements as well for the first two cases. Not so for the third.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.