重用Char指针数组

时间:2016-11-06 00:25:54

标签: c arrays malloc

假设我想"重复使用"一个char指针数组,就像在下面的程序中循环参数列表中给出的文件一样,循环文件中的行,将它们添加到动态分配的数组中,然后打印出来:

// includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// globals
int progReturn = 0;
int globalLineCounter = 0;



////// main
int main(int argc, char *argv[]) {
    FILE *fp;
    int i;


    // iterate files. first arg is the program name
    for (i = 1; i < argc; i++) {
        fp = fopen(argv[i], "r");
        if (fp == NULL) {
            fprintf(stderr, "The file '%s' did not exist.\n", argv[i]); 
            progReturn = 1;

        } else {

            // read lines from the file
            char line[256];

            // THE PROBLEM: I'd like to completely clear this array.            
            char **lines = malloc(16 * sizeof(char*));

            // iterate lines
            int fileLineCounter = 0;
            while (fgets(line, sizeof(line), fp)) {

                // remove newline
                strtok(line, "\n"); 

                // add lines to array
                lines[globalLineCounter] = malloc(256 * sizeof(char));
                strcpy(lines[globalLineCounter], line);
                //printf("%s\n", lines[globalLineCounter]); // tester

                fileLineCounter++;
                globalLineCounter++;                
            }
            // all lines read
            printf("The file '%s' had %d lines.\n", argv[i], fileLineCounter);

            // print the array
            int j=0;
            for (j=0; j<fileLineCounter; j++) {
                // PROBLEM: Garbage from the second file when it prints here. 
                printf("%s\n", lines[j]);
            }

            // delete lines, delete file
            memset(lines, 0, sizeof(*lines));
            fclose(fp);
        }  
    }
    // all files read

    return progReturn;
}

在第一个文件中,一切都没有问题。在第二个文件中,当我打印数组时,它显示不可打印的字符,以及第一个文件中的一些行。

可能导致此问题的原因是什么?我没有完全清除**lines

编辑:示例输入和输出:

输入文件foo:

This is a test
of the lineSort program
in order to
test its capabilities.
Lots of whitespace too!
aaa

bbb

         cccccc



aaa
ggggg
hhhhh
fffff
eeeee
ddddd
ppppp

输入文件栏:

aaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbb
zzzzzzzzzzzzzzzzz
ccccccccccccccccc
yyyyyyyyyyyyyyyyy

sortLine foo bar的输出:

The file 'foo' had 20 lines.





         cccccc
Lots of whitespace too!
This is a test
aaa
aaa
bbb
ddddd
eeeee
fffff
ggggg
hhhhh
in order to
of the lineSort program
ppppp
test its capabilities.
The file 'bar' had 5 lines.
(x▒▒
(x▒▒
Lots of whitespace too!
in order to
test its capabilities.

2 个答案:

答案 0 :(得分:1)

  1. char **lines初始化移到for循环之外。
  2. 将索引计数器i重命名为不同的名称。
  3. 在多个文件上反复调用lines[i] = malloc(...)将导致内存泄漏。考虑在free循环中使用for,或将这部分初始化移到for循环之外。

答案 1 :(得分:1)

    strcpy(lines[globalLineCounter], line);

这看起来像是你的主要问题。 globalLineCounter在所有输入文件中不断增加。

假设您的第一个输入文件包含10行,第二个文件包含5行。然后你的代码将创建一个动态数组(动态数组)并将第一个文件中的行存储在元素0 .. 9中(然后打印它们)。你永远不会释放任何已分配的内存,所以它在循环结束时都会泄漏。

对于第二个文件,您将创建另一个动态数组。您将第二个文件中的5行存储在元素10 .. 14中(通过globalLineCounter),然后打印元素0 .. 4({ {1}})。这些元素未初始化并包含垃圾。