c - 如何释放C中列表列表中的节点? (游戏)

标签 c list memory memory-management nodes

我正在制作一个游戏,我将图标放在网格的节点中,并且我正在使用这个结构:

typedef struct node{
    int x,y; //coordinates for graphics.h
    struct node *right, *left, *up, *down;
}Node;

我读取带有以下图标的文件:

int** ReadPixelFile(int *width, int *height, char name[50])
{
    FILE *fp;
    char address[150];
    int i, j;
    int **mesh;

    strcpy(address, "./Sprites/");
    strcat(address, nombre);
    strcat(address, ".pix");

    fp = fopen( address, "rb" );

    if(fp == NULL)
    { 
        printf("[Error]: cannot read .pix file\n");
        return 0;
    }

    //Read Width x Height
    fread(width, sizeof(int), 1, fp);
    fread(height, sizeof(int), 1, fp);


    AllocMemory(&mesh, *width, *height); //Here is when I allocate memory

    for(i = 0; i < *width; i++)
    {
        for(j = 0; j < *height; j++)
        {
            fread(*(mesh+ i) + j, sizeof(int), 1, fp);
        }
    }

    fclose(fp);

    return mesh;
}

我用这个分配内存:

void AllocMemory(int ***mesh, int iWidth, int iHeight)
{
    int i;

    *mesh = (int**)malloc(sizeof(int*)*iWidth);

    for(i = 0; i < iWidth; i++)
        *(*mesh+ i) = (int*)malloc(sizeof(int)*iHeight);

    if(!**mesh)
    {
        settextstyle(1,1,5);
        setcolor(RED);
        outtextxy(1280/3, 720/2, "Error: NO memory.");
    }
}

问题是我可以玩,图标不会有任何问题,这是游戏的屏幕截图:

https://scontent-dft4-1.xx.fbcdn.net/v/t1.0-9/15391071_10154209405046985_8141714891735886530_n.jpg?oh=4208ce0da45b76bed3ea5b85c2fbccee&oe=58B7F3CB

(我将其上传到 Facebook,因为我的学校不允许我使用 Imgur 之类的网站)

矩形代表网格,每个图标都是节点之一,随着时间的推移,我注意到游戏崩溃的频率越来越高,然后几乎每次我加载游戏时,我的老师都告诉我内存不足他告诉我释放内存,但我不知道如何释放内存,因为他从未告诉我们如何这样做以及在游戏的哪一部分释放内存,我每次打电话时都在考虑这样做gameOver()

void gameOver()
{
    setbkcolor(BLACK);
    cleardevice();
    setcolor(RED);
    settextstyle(GOTHIC_FONT, HORIZ_DIR, 7);
    outtextxy(640-textwidth("Game Over")/2, 360-textheight("Game Over")/2, "Game Over");
    settextstyle(GOTHIC_FONT, HORIZ_DIR, 3);
    outtextxy(640-textwidth("Press any letter to try again")/2, 600-textheight("Press any letter to return")/2, "Press any letter to return");


    if(kbhit())
    {
        setactivepage(0);
        cleardevice();
        Core(LIVES); //This is like the main function of the game
    }
}

Core()中,我调用ReadPixelFile(),这是我分配内存的时候...所以应该在调用Core()之前释放内存 并再次运行游戏?请帮忙,游戏已经完成,我只需要释放内存,这样它就不会崩溃。

谢谢。

最佳答案

在许多情况下,这个简单的技巧可以快速释放 C 中的内存并防止泄漏:

不要使用普通的 malloc 或 calloc,而是制作一个简单的包装器(例如,称为 MALLOC),它将向分配的 block 添加链接字段,并保留所有 block 的内部列表。 当您需要释放整个困惑时,请浏览此列表并对每个项目调用 free()。

变体:当您知道最大大小时,而不是列表,预先分配一个大缓冲区并仅使用一个指针从此缓冲区进行分配。您可以拥有多个此类池用于多种目的。 要释放所有内容,只需释放大缓冲区即可。或者,如果您想运行程序的另一次迭代,则保留它并重复使用。

看起来在你的情况下这是可行的。

/* C 类(class)的老师讨厌这个技巧,因为他们认为它会助长懒惰。对我来说,这是务实工程的例子,旨在防止某些问题,而不是让它进入并稍后进行调试。 */

“大缓冲区”变体的代码示例:


    /* somewhere in your main() */
    unsigned pool_size = 100000; /* maximum size */
    unsigned pool_allocated = 0;
    char *pool_ptr = (char*)malloc(pool_size);
    assert(pool_ptr);
    ....

    // Allocation function:
    void* my_alloc(unsigned size)
    {
       char *p;
       if (size == 0) 
           return NULL;
       if ( (pool_allocated + size) > pool_size )
          return NULL;
       p = &pool_ptr[pool_allocated];
       pool_alocated += size;
       return (void*)p;
    }


    void recycle_memory(void)
    {
        pool_allocated = 0; //that's simple :)
    }

关于c - 如何释放C中列表列表中的节点? (游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40998834/

相关文章:

java - 复制 Java 变量

c - 使用 Valgrind 写入和读取错误

c - golang os *File.Readdir 在所有文件上使用 lstat。可以优化吗?

c - Linux 执行程序; ls 无法访问 |,没有那个文件或目录

C - 我的贪婪算法不起作用 CS50x

python - 在 python 中初始化 2D 列表,因为我不知道确切的尺寸

列表 Monad : Consequences of the signature of List. flatMap(f: (A) ⇒ GenTraversableOnce[B])

c - 如果 int *p 是指向 int 的指针,为什么 &p 具有 **p 类型?

html - CSS 使用 List-Style-Image 调整文本在 HTML 列表中的位置

PHPExcel加载函数内存不足