我的程序将输入从文本文件中排序,然后将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]='\0';
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 :(得分:0)
在“\ 0”字符结束字符串之前,您有1个未经过初始化的字符(每次添加字符时,您都会增加i
计数器,因此在添加{{{}}时无需添加i+1
1}}。将'\0'
替换为line[i+1]='\0';
:
line[i]='\0';
答案 1 :(得分:0)
在SUSE linux中尝试使用gcc 4.3.4版编译程序时,我看到以下编译错误。
您的getline函数与头文件中的getline函数冲突。这不是您的问题的解决方案,但我觉得使用其他函数名称代替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