如何一次在C中释放所有内存

问题描述 投票:0回答:1

好的,我知道这个网站上有很多类似的问题,但是每个答案都是“那只是愚蠢,不要这样做”,我实际上是同意的。但是,不幸的是,我的项目有一个任务要创建一个函数,一次释放所有分配的内存,我想知道是否有一种方法可以不终止整个程序。这是我的整个代码,我不知道它会有多大帮助,但是粘贴整个代码总比什么都没有粘贴好。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    char line[1024];
    char subject[32];
    char platform[32];
    int time;
    int duration;
    struct node* next;
};

typedef struct node node_t;

extern node_t* newNode(char* line);
extern char* readLine(void);

node_t* newNode(char* line)
{
    node_t* result = malloc(sizeof(node_t));
    if (result == NULL)
    {
        fprintf(stderr, "Failed to allocate %zu bytes of memory\n", sizeof(node_t));
        exit(1);
    }
    strcpy(result->line, line);
    result->next = NULL;
    result->subject[0] = '\0';
    result->platform[0] = '\0';
    result->duration = -1;
    result->time = -1;
    return result;
}

char* readLine(void)
{
    size_t lenmax = 100;
    size_t len = lenmax;
    char* line = malloc(lenmax);
    char* linep = line;
    int c;

    if (line == NULL)
        return NULL;

    while ((c = fgetc(stdin)) != EOF && c != '\n')
    {
        if (--len == 0)
        {
            len = lenmax;
            char* linen = realloc(linep, lenmax *= 2);

            size_t diff = line - linep;
            if (linen == NULL)
            {
                free(linep);
                return NULL;
            }
            line = linen + diff;
            linep = linen;
        } 
        *line++ = c;
    }
    if (c == EOF || len == lenmax) /* or: if (c == EOF || line == linep) */
    {
        free(linep);
        return NULL;
    }
    *line = '\0';
    return linep;
}


void* splitLines(char* line)
{
    char* temp;
    char* Line;

    Line = strtok(line, ",");
    temp = Line;
    printf("%s <--------\n", temp);

    Line = strtok(NULL, ",");
    temp = Line;
    printf("%s <--------\n", temp);

    Line = strtok(NULL, ",");
    temp = Line;
    printf("%s <--------\n", temp);

    Line = strtok(NULL, ",");
    temp = Line;
    printf("%s <--------\n", temp);
}


void* terms(char* line)
{
    char* temp;
    char* Line;

    Line = strtok(line, ",");
    temp = Line;

    Line = strtok(NULL, ",");
    temp = Line;

    Line = strtok(NULL, ",");
    temp = Line;
    printf("%s <--------\n", temp);

    Line = strtok(NULL, ",");
    temp = Line;
}



int main(void)
{
    node_t* head = NULL;
    node_t* tail = NULL;
    int count = 0;

    char* line;

    while ((line = readLine()) != NULL)
    {
        char* Line = line;
        count++;
        node_t* node = newNode(line);
        if (count < 7)
        {
            //splitLines(line);
            //terms(line);
        }
        if (head == NULL)
            head = node;
        if (tail == NULL)
            tail = node;
        else
        {
            tail->next = node;
            tail = NULL;
        }
        printf("%s\n", node->line);

    }

    return 0;
}
c visual-studio free
1个回答
0
投票

其中一种方法是使用slab allocation方法,这将要求您编写一个自定义分配器。

基本上保留大的内存区域(平板),对象将在该内存区域内保留空间。

一旦不再需要该板中的对象,就可以取消分配该板。

分配示例:

struct slab {
     size_t size;
     size_t next_pointer;
     char* zone;
};


bool slab_init(struct slab *slab, const size_t size) {
    slab->zone = malloc(size);
    if(slab->zone == NULL) {return false;}
    slab->size = size;
    slab->next_pointer = 0;
    return true;
}

void slab_destroy(struct slab *slab) {
    free(slab->zone);
    slab->size = 0;
    slab->next_pointer = 0;
}

void* slab_malloc(struct slab *slab, size_t size) {
    if(size > slab->size) {
        return NULL;
    }
    if(slab->next_pointer < (slab->size - size)) {
        return NULL;
    }
    void* result=&(slab->zone[slab->next_pointer]);
    slab->next_pointer += size;
    return result;
}

[如果您仅可以使用标准功能,则也可以在堆栈上分配,尽管不建议这样做,因为通过_alloca_alloca很难从错误中进行恢复。

分配的变量只能在函数范围内使用。

© www.soinside.com 2019 - 2024. All rights reserved.