fprintf valgrind 错误
fprintf valgrind error
我的程序对文本文件中的输入进行排序,然后 fprintf
将其放入输出文件中。但是 valgrind 报告了 fprintf
调用的一些令人讨厌的错误,我不明白。任何提示是什么原因?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * mymalloc (int size)
{
static int count=0;
if (count<100)
{
count++;
return malloc(size);
}
else return NULL;
}
char* getline(FILE * infile)
{
int size = 16;
char * line = (char*)mymalloc(size);
int temp;
int i=0;
do
{
(temp = fgetc(infile));
if (temp !=EOF)
line[i++]=(char)temp;
if (i>=size)
{
size*=2;
line = (char*)realloc(line, size);
}
}while (temp != '\n' && temp !=EOF);
line[i+1]='[=10=]';
if (temp==EOF)
{
free(line);
return NULL;
}
return line;
}
void print_line (char * line)
{
printf("%s ", line);
}
int myCompare (const void * a, const void * b )
{
const char *pa = *(const char**)a;
const char *pb = *(const char**)b;
return strcmp(pa,pb);
}
int main (int argc, char* argv[])
{
FILE * infile;
FILE * outfile;
infile = fopen(argv[1], "r");
if (infile==NULL)
{
printf("Error");
exit(3);
}
outfile = fopen(argv[2], "w");
if (outfile==NULL)
{
printf("Error");
exit(3);
}
char * line;
char **all_lines;
int nlines=0;
while((line=getline(infile))!=NULL)
{
print_line(line);
nlines++;
free(line);
}
all_lines=mymalloc(nlines*sizeof(char*));
rewind(infile);
int j=0;
printf("%d\n\n", nlines);
while((line=getline(infile))!=NULL)
{
all_lines[j]=line;
j++;
}
qsort(all_lines, nlines, sizeof(char*), myCompare);
for (int i=0; i<nlines;i++)
{
//print_line(all_lines[i]);
fprintf(outfile, "%s " , all_lines[i]);
}
for(int i =0; i<nlines;i++)
{
free(all_lines[i]);
}
free(all_lines);
fclose(infile);
fclose(outfile);
return 0;
}
Valgrind 错误:
==3678== 11 errors in context 1 of 2:
==3678== Conditional jump or move depends on uninitialised value(s)
==3678== at 0x40BA4B1: vfprintf (vfprintf.c:1601)
==3678== by 0x40C0F3E: fprintf (fprintf.c:33)
==3678== by 0x8048964: main (sort_lines.c:99)
==3678== Uninitialised value was created by a heap allocation
==3678== at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==3678== by 0x80486FB: mymalloc (sort_lines.c:11)
==3678== by 0x804871C: getline (sort_lines.c:18)
==3678== by 0x8048905: main (sort_lines.c:87)
==3678==
==3678==
==3678== 11 errors in context 2 of 2:
==3678== Conditional jump or move depends on uninitialised value(s)
==3678== at 0x40BA4B1: vfprintf (vfprintf.c:1601)
==3678== by 0x40C0F7F: printf (printf.c:35)
==3678== by 0x80487B8: print_line (sort_lines.c:44)
==3678== by 0x804887D: main (sort_lines.c:77)
==3678== Uninitialised value was created by a heap allocation
==3678== at 0x4024D12: realloc (vg_replace_malloc.c:476)
==3678== by 0x8048766: getline (sort_lines.c:30)
==3678== by 0x804889A: main (sort_lines.c:75)
==3678==
--3678--
--3678-- used_suppression: 17 dl-hack3-cond-1
==3678==
==3678== ERROR SUMMARY: 22 errors from 2 contexts (suppressed: 17 from 8)
在结束字符串的“\0”字符之前有 1 个未初始化的字符(每次添加字符时,您 post 增加 i
计数器,因此无需添加 i+1
添加 '[=13=]'
时。将 line[i+1]='[=14=]';
替换为 line[i]='[=15=]';
:
char* getline(FILE * infile)
{
int size = 16;
char * line = (char*)mymalloc(size);
int temp;
int i=0;
do
{
(temp = fgetc(infile));
if (temp !=EOF)
line[i++]=(char)temp;
if (i>=size)
{
size*=2;
line = (char*)realloc(line, size);
}
}while (temp != '\n' && temp !=EOF);
line[i]='[=10=]'; //// <- change here
if (temp==EOF)
{
free(line);
return NULL;
}
return line;
}
尝试在 SUSE linux 中使用 gcc 4.3.4 版编译您的程序时,我看到以下编译错误。
您的 getline 函数与 header 中的 getline 函数冲突 file.This 无法解决您的问题,但我觉得最好使用其他函数名称代替 getline。
http://man7.org/linux/man-pages/man3/getline.3.html
user/home> gcc -g so1.c
so1.c:16: error: conflicting types for 'getline'
/usr/include/stdio.h:653: error: previous declaration of 'getline' was here
so1.c: In function 'main':
so1.c:98: error: 'for' loop initial declaration used outside C99 mode
so1.c:104: error: redefinition of 'i'
so1.c:98: error: previous definition of 'i' was here
so1.c:104: error: 'for' loop initial declaration used outside C99 mode
我的程序对文本文件中的输入进行排序,然后 fprintf
将其放入输出文件中。但是 valgrind 报告了 fprintf
调用的一些令人讨厌的错误,我不明白。任何提示是什么原因?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * mymalloc (int size)
{
static int count=0;
if (count<100)
{
count++;
return malloc(size);
}
else return NULL;
}
char* getline(FILE * infile)
{
int size = 16;
char * line = (char*)mymalloc(size);
int temp;
int i=0;
do
{
(temp = fgetc(infile));
if (temp !=EOF)
line[i++]=(char)temp;
if (i>=size)
{
size*=2;
line = (char*)realloc(line, size);
}
}while (temp != '\n' && temp !=EOF);
line[i+1]='[=10=]';
if (temp==EOF)
{
free(line);
return NULL;
}
return line;
}
void print_line (char * line)
{
printf("%s ", line);
}
int myCompare (const void * a, const void * b )
{
const char *pa = *(const char**)a;
const char *pb = *(const char**)b;
return strcmp(pa,pb);
}
int main (int argc, char* argv[])
{
FILE * infile;
FILE * outfile;
infile = fopen(argv[1], "r");
if (infile==NULL)
{
printf("Error");
exit(3);
}
outfile = fopen(argv[2], "w");
if (outfile==NULL)
{
printf("Error");
exit(3);
}
char * line;
char **all_lines;
int nlines=0;
while((line=getline(infile))!=NULL)
{
print_line(line);
nlines++;
free(line);
}
all_lines=mymalloc(nlines*sizeof(char*));
rewind(infile);
int j=0;
printf("%d\n\n", nlines);
while((line=getline(infile))!=NULL)
{
all_lines[j]=line;
j++;
}
qsort(all_lines, nlines, sizeof(char*), myCompare);
for (int i=0; i<nlines;i++)
{
//print_line(all_lines[i]);
fprintf(outfile, "%s " , all_lines[i]);
}
for(int i =0; i<nlines;i++)
{
free(all_lines[i]);
}
free(all_lines);
fclose(infile);
fclose(outfile);
return 0;
}
Valgrind 错误:
==3678== 11 errors in context 1 of 2:
==3678== Conditional jump or move depends on uninitialised value(s)
==3678== at 0x40BA4B1: vfprintf (vfprintf.c:1601)
==3678== by 0x40C0F3E: fprintf (fprintf.c:33)
==3678== by 0x8048964: main (sort_lines.c:99)
==3678== Uninitialised value was created by a heap allocation
==3678== at 0x4024C1C: malloc (vg_replace_malloc.c:195)
==3678== by 0x80486FB: mymalloc (sort_lines.c:11)
==3678== by 0x804871C: getline (sort_lines.c:18)
==3678== by 0x8048905: main (sort_lines.c:87)
==3678==
==3678==
==3678== 11 errors in context 2 of 2:
==3678== Conditional jump or move depends on uninitialised value(s)
==3678== at 0x40BA4B1: vfprintf (vfprintf.c:1601)
==3678== by 0x40C0F7F: printf (printf.c:35)
==3678== by 0x80487B8: print_line (sort_lines.c:44)
==3678== by 0x804887D: main (sort_lines.c:77)
==3678== Uninitialised value was created by a heap allocation
==3678== at 0x4024D12: realloc (vg_replace_malloc.c:476)
==3678== by 0x8048766: getline (sort_lines.c:30)
==3678== by 0x804889A: main (sort_lines.c:75)
==3678==
--3678--
--3678-- used_suppression: 17 dl-hack3-cond-1
==3678==
==3678== ERROR SUMMARY: 22 errors from 2 contexts (suppressed: 17 from 8)
在结束字符串的“\0”字符之前有 1 个未初始化的字符(每次添加字符时,您 post 增加 i
计数器,因此无需添加 i+1
添加 '[=13=]'
时。将 line[i+1]='[=14=]';
替换为 line[i]='[=15=]';
:
char* getline(FILE * infile)
{
int size = 16;
char * line = (char*)mymalloc(size);
int temp;
int i=0;
do
{
(temp = fgetc(infile));
if (temp !=EOF)
line[i++]=(char)temp;
if (i>=size)
{
size*=2;
line = (char*)realloc(line, size);
}
}while (temp != '\n' && temp !=EOF);
line[i]='[=10=]'; //// <- change here
if (temp==EOF)
{
free(line);
return NULL;
}
return line;
}
尝试在 SUSE linux 中使用 gcc 4.3.4 版编译您的程序时,我看到以下编译错误。
您的 getline 函数与 header 中的 getline 函数冲突 file.This 无法解决您的问题,但我觉得最好使用其他函数名称代替 getline。 http://man7.org/linux/man-pages/man3/getline.3.html
user/home> gcc -g so1.c
so1.c:16: error: conflicting types for 'getline'
/usr/include/stdio.h:653: error: previous declaration of 'getline' was here
so1.c: In function 'main':
so1.c:98: error: 'for' loop initial declaration used outside C99 mode
so1.c:104: error: redefinition of 'i'
so1.c:98: error: previous definition of 'i' was here
so1.c:104: error: 'for' loop initial declaration used outside C99 mode