我有一个代码,应该为监管目的生成一个曲线,工作正常。简单的calloc和FILE代码在窗口上随机崩溃
来自Linux环境,我真的不知道如何在Windows上编码(我不得不在工作中使用),所以我继续下载Code :: Blocks和MinGW。
现在的问题如下:我的代码工作,但有时它崩溃。我在Linux上尝试过,我没有任何问题,但是在Windows上,它有时会起作用,有时它不会告诉我这一点。
Problem signature:
Problem Event Name: APPCRASH
Application Name: TabGen.exe
Application Version: 0.0.0.0
Application Timestamp: 02881c68
Fault Module Name: ntdll.dll
Fault Module Version: 6.1.7601.23418
Fault Module Timestamp: 5708a73e
Exception Code: c0000005
Exception Offset: 00032a62
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 2055
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
说实话,我不太明白。我试图查找“异常代码c0000005窗口”,这显然意味着“访问违规”,但我不明白它来自哪里,因为如所述,有时它有时不工作。
与MinGW有关吗?我在代码中做错了什么?我有一个函数mk_cp_table(它始终使用4096个值,这就是为什么它不是一个函数参数),但我很积极,这是没问题的。
此外,如果你想知道为什么我使用calloc而不是数组,这又是因为我不明白的错误。最后一个函数末尾的fprintf不起作用,如果我在数组中有我的值(但是单独使用printf显示它们完美工作,但fprintf只会在我运行它时留下空白文件)。
任何想法?
我的代码:
#include <stdio.h>
#include <stdlib.h>
void tab2file(FILE*, int, int*);
void mk_cp_table (int*, float, float, float, int, float, float, int);
int main (void)
{
FILE* cp_file = fopen("cp_table.txt", "w");
if (cp_file == NULL)
{
perror("failed fopen for cp_file\n");
exit(EXIT_FAILURE);
}
int* cp_table = calloc((size_t) 4096, sizeof (int));
if (cp_table == NULL)
{
fclose(cp_file);
perror("failed calloc for cp_table\n");
exit(EXIT_FAILURE);
}
/* Generate curves */
mk_cp_table(
cp_table,
0.142, /* scale */
20, /* zeroed distance 0 (negative part) */
0.04, /* slope 0 */
120, /* range 0 */
20, /* zeroed distance 1 (positive part) */
0.04, /* slope 1 */
120 /* range 1 */
);
printf("\ntable generated\n");
tab2file(cp_file, 4096, cp_table);
printf("\ntables written to files\n");
fclose(cp_file);
free(cp_table);
return EXIT_SUCCESS;
}
void tab2file(FILE* file, int size, int* table)
{
int i;
for (i = 0; i < size; i++)
fprintf(file, "%d\n", table[i]);
}
void mk_cp_table(int* table, float scale, float zeroes_0, float slope_0, int range_0, float zeroes_1, float slope_1, int range_1)
{
int i;
int value;
zeroes_0 = zeroes_0/scale;
zeroes_1 = zeroes_1/scale;
for (i = 0; i < 2048; i++)
{
if (i < zeroes_1)
value = 0;
else
value = (i - zeroes_1) * slope_1;
if (value > range_1)
value = 127;
table[i] = 0;
}
for (i = 0; i < 2048; i++)
{
if (i < zeroes_0)
value = 0;
else
value = (zeroes_0 - i) * slope_0;
if (value < -range_0)
value = -127;
table[4096 - i] = value;
}
}
'table [4096 -i] = value'当'i' == 0时,它写入'table'的范围之外。 – Michael
@迈克尔:那是心灵感应,我正要评论完全相同的东西! –
@Luda Otaku您正在写出界限,并在您的程序的最后一行遇到类似于seg错误的窗口。 – gowrath