gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:30:01 27 4
gpt4 key购买 nike

又是新手!我在测试的一些代码中遇到了一些麻烦,以便尝试将数组写入文件,然后希望了解如何将每行和每列中的每个元素相乘(将每个元素写入一定次数按行和列)。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)

最佳答案

问题出在你的循环中:

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++;
}

作为旁注,没有必要 free() 你的 FILE 指针

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

因为它没有被分配以防出错(你本质上是在做 free(NULL) )

关于c - 尝试将数组写入文件时检测到堆栈粉碎错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35154764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com