English 中文(简体)
瓦列格里德与我一样,担任联系名单的职务
原标题:Valgrind doesn t like my pop() function for a linked list

• Im在使用Ncurs的C类终端天球游戏上工作,我用以下代码把天球作为双倍链接清单:

typedef struct {
    int y; 
    int x;
} coordinates;

bool coordsequal(coordinates c1, coordinates c2);

typedef struct {
    coordinates coords;
    char attire;
} object;

typedef object part;

/* Snake: Doubly Linked list of Parts */
typedef struct dllsnake {
    part part;
    struct dllsnake *prev;
    struct dllsnake *next;
} snake; 

由此,我确定了一项职能,将最后一项内容放在与天线相连的名单中:

snake *pop(snake *head) {
    if (head == NULL) {
        // List is empty, nothing to pop
        return NULL;
    }

    snake *ptr; // line 84
    for (ptr = head; ptr->next != NULL; ptr = ptr->next); // line 85

    snake *new_tail = ptr->prev; // line 87
    new_tail->next = NULL;
    free(ptr);

    return new_tail;
}

This function works as intended in the snake game, deleting the snake s tail when prompted to. However, when I run my snake game with valgrind, valgrind has many complaints about this function, including:

  • Invalid read of size 8 at lines 84, 85, and 87
  • Invalid free() / delete / delete[] / realloc() at line 87
  • Addresses 0x4b52d10, 0x4b52d20, and 0x4b52d28 are 0 bytes inside a block of size 32 free d, line 87

更有甚者,当天花游戏时,波人完全停止工作。

我don不 地知道与这些信息做什么! 首先,我看不出我不准备阅读的记忆。 第二,如果自由声明在后面有两条线,那么为什么要看第87条? 我don不知最后一点。 任何帮助都会受到高度赞赏。

问题回答
  1. 如果您的双重联系名单有一个点,如果head->prevNUL,则您的方案将载于new_tail->next

  2. <代码>pop(> 通常将最后的节点重新命名,因此,您可能不会免费。

  3. <代码>pop(:它不是错的,而是你头部通过并交还尾的奇怪之处。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签