C编程 - 基于矩阵的功能不起作用

时间:2013-05-13 17:14:13

标签: c string memory matrix valgrind

在我正在编写的程序中,我有这个函数,它读取输入并将每一行存储在一个字符数组的条目中。 n是先前在输入中给出的数字,表示要读取的行数。

char **linelist(int n)
{
    int i, j;
    char **list;
    list = calloc(n, sizeof(char *));
    for (j = 0; j < n; j++){
        list[j] = calloc(MAX_STR, sizeof(char));
    }
    for (i = 0; i < n; i++){
        fgets(list[i], (MAX_STR), stdin);
    }
    return list;
}

这个函数似乎工作正常,因为某个命令的输出应该给输入输出总是匹配测试中的假设输出。

然后,我们有以下函数,它接受前面提到的数组,并进一步将行拆分为单词,生成一个二维矩阵,其中有n行,对应于行数在输入中,因此,它们在list中的数字和任意数量的行,每行中的每个行的单词都存储在其中。

char ***createmat(int n, char **list)
{
    char ***matrix;
    int i, x, j, k, l, y, z;
    char *buffer;
    const char separators[] = {' ','\t',',',';','.','?','!','"','\n'};
    printf("F0\n");
    buffer = calloc(MAX_STR, sizeof(char));
    y = 0;

    matrix = calloc(n, sizeof(char**));
    for(z = 0; z < n; z++) {
        matrix[z] = calloc(n, sizeof(char*)); // line 100
        for(i = 0; i < n; i++) {
            matrix[z][i] = calloc(MAX_STR, sizeof(char));
        }
    }
    printf("F1\n");
    for(x = 0; x < n; x++){
        for(j = 0, l = 0; list[x][j] != '\0'; j++, l++){
            buffer[l] = tolower(list[x][j]);
            for(k = 0; k < 9; k++){
                if(list[x][j] == separators[k]){
                    if(l != 0){
                        buffer[l] = '\0';
                        printf("F2\n");
                        stringcpy(buffer,matrix[x][y++]);  //line 114
                        printf("F3\n");
                    }
                    l = -1;
                }
            }
        }
        matrix[x][y+1] = "\n";
    }
    printf("F4\n");
    return matrix;
}

这是一个函数,它从为matrix分配内存开始,这个过程在Valgrind中没有引发任何错误。问题似乎来自于应该分离和存储单词的部分。根据测试,当每行只有一个单词时似乎有效,但是当任何行有多个单词时则不行。 printf语句用于调试目的并在终端中运行以下输入:

3  //what the functions receive as int n - the number of lines
the sky
is pink and yellow and
not uncute

提供以下输出:

F0
F1
F2
F3
F2
F3
F2
F3
F2
Segmentation fault (core dumped)

至于在Valgrind中运行它会返回以下内容:

==7599== Invalid read of size 8
==7599==    at 0x400D01: createmat (proj.c:114)
==7599==    by 0x4015A7: main (proj.c:241)
==7599==  Address 0x5201428 is 0 bytes after a block of size 72 alloc'd
==7599==    at 0x4C2ABB4: calloc (vg_replace_malloc.c:593)
==7599==    by 0x400BBC: createmat (proj.c:100)
==7599==    by 0x4015A7: main (proj.c:241)
==7599== 
==7599== Invalid read of size 1
==7599==    at 0x40087D: stringcpy (proj.c:12)
==7599==    by 0x400D16: createmat (proj.c:114)
==7599==    by 0x4015A7: main (proj.c:241)
==7599==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

stringcpy功能如下:

void stringcpy(char *s, char *t)
{
    while ((*s++ = *t++) != '\0'); //line 12
}

我一直在试图弄清楚什么是错的,但无济于事,因为我在C中的经验很少。任何帮助都会受到赞赏。谢谢。

2 个答案:

答案 0 :(得分:1)

一个可能的问题是变量y似乎永远不会重置为零。看起来好像应该在到达行尾时重置为零。

另一种可能性是,如果给定行中有超过n个单词,那么这将导致写入已分配内存的末尾。

修改根据stringcpy的来源添加,您看起来在调用中有向后的参数。我认为应该是:

stringcpy(matrix[x][y++],buffer);  //line 114

请注意,以下行会导致内存泄漏(常量字符串"\n"的分配会导致指向该位置中已分配内存的指针丢失:

    matrix[x][y+1] = "\n";

答案 1 :(得分:0)

如果你试图复制指针所持有的值,你可以使用C的strcpy函数我想,在这里你可以找到关于函数的参考。但我不确定你的stringcpy方法是否导致错误。