c - 我的(自定义)程序如何泄漏内存?我正在为 pset5 做准备

标签 c memory-leaks valgrind realloc cs50

我试图了解内存分配和指针是如何工作的,因为我发现 CS50 (pset5) 的问题集太复杂了。

我制作了一个简单的程序,从数组中读取字符,并将它们写入新的文本文件和终端中。

程序可以运行,但是内存泄漏。 特别是对于字符串中遇到的每个\n,valgrind 表示它会再丢失 1 个 block 中的内存。对于字符串(char *c)中的每个字符,它表明又泄漏了 1 个字节。

我做错了什么?

终端图片链接:/image/ANtAs.png

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

  int main (void)
  {

     FILE *fp;
     char *c = "One\nTwo\n";

     // Open file for writing (reading and writing works too, we can use 'w+' for that).
     fp = fopen("file.txt", "w");

     // Write data to the file.
     fwrite(c, strlen(c), 1, fp);

     // Seek to the beginning of the file
     fseek(fp, 0, SEEK_SET);

     // close file of the file pointer (the text file).
     fclose(fp);

     // initialize a counter for the amount of characters in the current word that is being read out of the file.
     int char_count = 0;

     // initialize an address for the first character in a string.
     char *buffer_temp_word = NULL;

     // Read and display data, using iterations over each character.
     // Open the file in read mode.
     fp = fopen("file.txt", "r");

     // initiate a for loop.
     // condition 1: getting a character from the fp stream does not equal reaching the end of the file
     // condition 2: the amount of iterations is not above 60 (failsafe against endless loops).
     for (int i = 0; fgetc(fp) != EOF && i <= 60 ; i++)
     {
        //add a counter to the amount of characters currently read.
        char_count++;
        // seek the pointer 1 place back (the 'IF' function moves the pointer forward 1 place forward for each character).
        fseek(fp , -1L, SEEK_CUR);
        // get the character value of the current spot that the pointer of the read file points to.
        char x = fgetc(fp);
        buffer_temp_word = realloc(buffer_temp_word, (sizeof(char)) * char_count);

        //the string stores the character on the correct place
        //(the first character starts at memory location 0, hence the amount of characters -1)
        buffer_temp_word[char_count - 1] = x;

        // check for the end of the line (which is the end of the word).
        if(x == '\n')
        {
           //printf("(end of line reached)");
           printf("\nusing memory:");

           // iterate trough characters in the memory using the pointer + while loop, option 2.
           while(*buffer_temp_word != '\n')
           {
              printf("%c", *buffer_temp_word);
              buffer_temp_word++;
           }

           printf("\nword printed succesfully");
           // reset the pointer to the beginning of the buffer_temp_word string (which is an array actually).
           buffer_temp_word = NULL;
           free(buffer_temp_word);

           // reset the amount of characters (for the next word that will be read).
           char_count = 0;
        }
        printf("%c", x);
     }
     fclose(fp);
     free(buffer_temp_word);
     return(0);
  }

最佳答案

在释放 buffer_temp_word 之前将其设置为 NULL:

// reset the pointer to the beginning of the buffer_temp_word string (which is an array actually).
buffer_temp_word = NULL;
free(buffer_temp_word);

如果您使用 clang 的静态分析器,它可以引导您完成代码中的路径以显示内存泄漏。

此外,将指针设置为 NULL 不会将其重置为它指向的数组的起始位置,而是将其设置为 NULL。考虑使用 for 循环而不是 while 循环,并使用计数器来索引数组:

for(int j = 0; buffer_temp_word[j] != '\n'; ++j)
{
    printf("%c", buffer_temp_word[j]);
}

然后不要将 buffer_temp_word 设置为 NULL,也不要在此循环后立即释放它。该程序已设置为重新分配它或稍后释放它。

关于c - 我的(自定义)程序如何泄漏内存?我正在为 pset5 做准备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53068019/

相关文章:

c - 埃拉托斯特尼筛法,素数 C

IOS7 - Xcode5 - 没有代码的内存泄漏

c++ - SWIG - 包装 std::pair 字符串时内存泄漏

windows - 使用 gflags +ust 可以更快地增加进程内存

C++ 内存泄漏

c - 某些 C 实现是否定义了 string 关键字?

c - 我想仅使用一个 for 循环同时打印算术级数和几何级数,但我只能打印一个

c - 在 C 中构建二维字符串数组的引用运算符

c++ - 比较应该相同的 C++ 程序运行的配置文件以检测分歧

c - 在应用程序退出之前关闭的共享库上使用 Valgrind