c - malloc 和 free 问题

标签 c memory-management malloc free

所以我有这段代码,我运行了几次:

void svnViewStatus()
{
    FILE *file;
    int i, j, k, q=0, ok;
    char mystring[100000], *vect[100000], *var, last[12], first[12];
    file = fopen("db_svnViewStatus.txt", "r");
    if(file == NULL)
    {
        printf("error");
    }
    else
    {
        i=1;
        while(fgets(mystring, 100000, file))
        {
            if( i > 1) 
            {
                j=1;
                var = strtok(mystring, ";");
                while(var != NULL)
                {
                    if(j==3)
                    {
                        if(i == 2)
                        {
                            strcpy(first, var);
                        }
                        strcpy(last, var);
                    }
                    if(j == 2)
                    {
                        if(q != 0)
                        {
                            ok=1;
                            for(k=0; k<q; k=k+2)
                            {
                                if(strcmp(vect[k], var) == 0)
                                {
                                    *vect[k+1]++;
                                    ok=0;
                                }
                            }
                            if(ok == 1)
                            {
                                vect[q] = malloc(strlen(var)+1);
                                strcpy(vect[q], var);
                                vect[q+1] = malloc(sizeof(int));
                                *vect[q+1] = 1;
                                q = q+2;
                            }
                        }
                        else
                        {
                            vect[q] = malloc(strlen(var)+1);
                            strcpy(vect[q], var);
                            vect[q+1] = malloc(sizeof(int));
                            *vect[q+1] = 1;
                            q = q+2;
                        }
                    }
                    j++;
                    var = strtok(NULL, ";");
                }
            }
            i++;
        }
    }
    fclose(file);
    printf("nr: %d \n", i-2);
    printf("first: %s \n", first);
    printf("last: %s \n", last);
    for(i=0; i<q; i = i+2)
    {
        printf("User %s: %d \n", *vect[i], *vect[i+1]);
    }
    for(i=0; i<q; i=i+1)
    {
         free(vect[i]);
    }

}

在 db_svnViewStatus.db 中我有:

NumeRepo:CitateWoodyAllen;DataCreat:12 Nov 2011;Detinator:Ioana;Descriere:Citate ale faimosului regizor Woody Allen
1;Ioana;12 Nov 2011;Woody Allen;What if everything is an illusion and nothing exists? In that case, I definitely overpaid for my carpet.
2;Mihai;12 Nov 2011;Woody Allen;The lion and the calf shall lie down together but the calf won't get much sleep
3;Mihai;13 Nov 2011;Woody Allen;Eighty percent of success is showing up
4;Cristi;23 Nov 2011;Woody Allen;It is impossible to travel faster than the speed of light, and certainly not desirable, as one's hat keeps blowing off
5;Ioana;25 Nov 2011;Woody Allen;I had a terrible education. I attended a school for emotionally disturbed teachers.
6;Cristi;25 Nov 2011;Woody Allen;I will not eat oysters. I want my food dead. Not sick. Not wounded. Dead.

但是我明白了:

“检测到堆损坏:在 0x000032E90 处的正常 block (#54) 之后。 CRT 检测到应用程序在堆缓冲区结束后写入内存。”

有什么帮助吗?

另外,分配内存后是否应该使用free?为什么?

最佳答案

您应该为终止零分配内存,如下所示:

vect[q] = malloc(strlen(var)+1);

如果您未能分配额外的字节,strcpy 将写入已分配 block 的末尾,导致堆损坏。

关于c - malloc 和 free 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8696818/

相关文章:

c++ - C API 应该包含在可分发库中吗?

memory-management - 内存不足错误。如何定位?怎么解决?

java - 将值存储为变量或再次调用方法更好吗?

c - 结构中数组的 malloc 作为参数传递

C函数返回指向结构的指针

c++ - realloc() 的性能消耗

c - 为什么 printf() 是一个不纯的函数?

C 矩阵和 vector 的动态分配

java - Neo4j java 高 cpu 使用率仍然是 392%

c++ - 寻找 C/C++ 语言和标准库规范