C 尝试将数组写入文件时检测到堆栈粉碎错误

C 尝试将数组写入文件时检测到堆栈粉碎错误,c,arrays,C,Arrays,又是新手了! 我在测试一段代码时遇到了一些问题,因为我正在尝试将数组写入一个文件,然后,希望能够理解如何将每行和每列中的每个元素相乘(按行和列将每个元素写入一定次数)。 Valgrind报告说,我的漏洞大约为34,这是第二个fopen(),我不明白为什么会出现这种情况以及如何修复它: #include <stdio.h> #include <stdlib.h> #define SIZE 2 int main(int argc, char

又是新手了! 我在测试一段代码时遇到了一些问题,因为我正在尝试将数组写入一个文件,然后,希望能够理解如何将每行和每列中的每个元素相乘(按行和列将每个元素写入一定次数)。 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);
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)

问题在您的循环中:

for (row = 0; row < SIZE * SIZE; row++)
    {
        for(column = 0; column < SIZE * SIZE; column++)
        {
            inArray[row][column] = count++;
        }
    }
作为旁注,没有必要释放()文件指针

if(infileptr == NULL)
{
    printf("Could not create file %s.\n", argv[1]);
    free(infileptr);
    return 1;
}   

因为它在发生错误时没有被分配(你基本上是在做
free(NULL)

实际上,那么它不仅是“不需要”(这意味着
free
ing是可选的),而且他也不能。@Olaf就我所知,free(NULL)是一个NOP。free()手册页中也说明了这一点:“free()函数释放ptr指向的内存空间,该空间必须是通过以前调用malloc()、calloc()或realloc()返回的。否则,或者如果以前调用过free(ptr),则会发生未定义的行为。如果ptr为NULL,则不会执行任何操作。”虽然这是正确的,代码的存在意味着OP的一些更深层次的误解
free
ing一个明显的空指针-但我承认,我应该使用较弱的“应该”。挑剔:他不释放“文件指针”,而是试图释放它指向的对象。(这恰好被
free
-顺便说一句:通常最好参考一级参考,即这里的C标准)。是的,你说得对。这就是为什么我在回答中提到了这一点,尽管这不是问题所在。感谢您对使用C标准的建议。下次我会努力记住的。
for (i = 0; i < SIZE * SIZE; i++)
    {
        *(inArray + i) = count++;
    }
if(infileptr == NULL)
{
    printf("Could not create file %s.\n", argv[1]);
    free(infileptr);
    return 1;
}