简体   繁体   English

优先队列分割故障

[英]priority queue segmentation fault

I am using a priority queue with a double as the priority. 我正在使用优先级队列,其优先级为double。 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 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 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;
        }
     }
}

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. 类型N指向您的结构的指针 ,而不是结构本身的指针 That means that sizeof(N) is likely to be much smaller than the structure, meaning that you're not allocating enough memory. 这意味着sizeof(N)可能比结构小得多,这意味着您没有分配足够的内存。

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. 并且您可能会看到一系列格式为4 328 32 ,显示出,尽管您已分配了四个或八个字节,但您需要 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. 尽管前两者可以处理结构大小的任何更改,但是即使将new声明为完全不同的结构,也可以保留下来,而不必在多个位置更改信息。

By that I mean, if you change the type of new thus: 我的意思是,如果您这样更改new的类型,则:

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. 第三则不是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM