检测到堆栈碎片错误试图将数组写入文件

时间:2016-02-02 13:25:02

标签: c arrays

再次来到这里! 我在测试的一些代码中遇到一些麻烦,以便尝试将数组写入文件,然后,希望了解如何将每个元素和每列中的每个元素相乘(将每个元素写入一定次数按行和列)。 Valgrind报告我的泄漏是34,这是第二次fopen(),我不明白为什么会这样,以及如何解决它:

    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 2 

    int main(int argc, char * argv[])
    {   
       //read commandline args.
    if (argc != 3)
    {
        printf("Usage: %s <input file> <output file>\n", argv[0]);
        return 1;
    }

    //filenames
    char * infile = argv[1];
    char * outfile = argv[2];

    //create files.
    FILE * infileptr = fopen(infile, "w");
    if(infileptr == NULL)
    {
        printf("Could not create file %s.\n", argv[1]);
        free(infileptr);
        return 1;
    }   
    FILE * outfileptr = fopen(outfile, "w");
    if(outfileptr == NULL)
    {
        printf("Could not create file %s.\n", argv[2]);
        free(outfileptr);
        return 1;
    }

    //fill the infile with an array contents.
    int inArray[SIZE][SIZE];
    int count = 0, row, column;
    //intialize the array.
    for (row = 0; row < SIZE * SIZE; row++)
    {
        for(column = 0; column < SIZE * SIZE; column++)
        {
            inArray[row][column] = count++;
        }
    }
    //write to the infile.
    for (row = 0; row < SIZE * SIZE; row++)
    {
        for(column = 0; column < SIZE * SIZE; column++)
        {
            fprintf(infileptr, "%i", inArray[row][column]);
        }
    } 
    fclose(infileptr);

当我尝试运行它时,我得到以下内容:

    ./multiplyarray infile.txt outfile.txt
    *** stack smashing detected ***: ./multiplyarray terminated
    Aborted (core dumped)

Valgrind报道:

==12385== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==12385== Command: ./multiplyarray infile.txt outfile.txt
==12385== 
*** stack smashing detected ***: ./multiplyarray terminated
==12385== 
==12385== Process terminating with default action of signal 6 (SIGABRT)
==12385==    at 0x4E6D267: raise (raise.c:55)
==12385==    by 0x4E6EEC9: abort (abort.c:89)
==12385==    by 0x4EB0C52: __libc_message (libc_fatal.c:175)
==12385==    by 0x4F50E8B: __fortify_fail (fortify_fail.c:38)
==12385==    by 0x4F50E2F: __stack_chk_fail (stack_chk_fail.c:28)
==12385==    by 0x400884: main (multiplyarray.c:62)
==12385== 
==12385== HEAP SUMMARY:
==12385==     in use at exit: 552 bytes in 1 blocks
==12385==   total heap usage: 2 allocs, 1 frees, 1,104 bytes allocated
==12385== 
==12385== 552 bytes in 1 blocks are still reachable in loss record 1 of 1
==12385==    at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12385==    by 0x4EA711C: __fopen_internal (iofopen.c:69)
==12385==    by 0x400784: main (multiplyarray.c:34)
==12385== 
==12385== LEAK SUMMARY:
==12385==    definitely lost: 0 bytes in 0 blocks
==12385==    indirectly lost: 0 bytes in 0 blocks
==12385==      possibly lost: 0 bytes in 0 blocks
==12385==    still reachable: 552 bytes in 1 blocks
==12385==         suppressed: 0 bytes in 0 blocks
==12385== 
==12385== For counts of detected and suppressed errors, rerun with: -v
==12385== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Aborted (core dumped)

1 个答案:

答案 0 :(得分:4)

问题出在你的循环中:

for (row = 0; row < SIZE * SIZE; row++)
    {
        for(column = 0; column < SIZE * SIZE; column++)
        {
            inArray[row][column] = count++;
        }
    }

由于您将inArray声明为int inArray[SIZE][SIZE];,因此您尝试写入超出声明的大小。请注意,您使用SIZE * SIZE作为循环的分隔符,您应该只使用row < SIZEcolumn < SIZE

我猜你与另一个用于初始化这种矩阵的常见构造混淆了。这样的事情对你也有用:

for (i = 0; i < SIZE * SIZE; i++)
    {
        *(inArray + i) = count++;
    }

作为旁注,没有必要在

中释放()你的FILE指针
if(infileptr == NULL)
{
    printf("Could not create file %s.\n", argv[1]);
    free(infileptr);
    return 1;
}   

因为在发生错误的情况下没有分配(你必须正在做free(NULL)