c - fprintf valgrind 错误

标签 c printf valgrind

我的程序对文本文件中的输入进行排序,然后将其 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”字符之前有 1 个未初始化的字符(每次添加字符时,都会增加 i 计数器,因此无需添加 i+1 添加'\0'时。将line[i+1]='\0';替换为line[i]='\0 ';:

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]='\0'; //// <- change here
if (temp==EOF)
    {
        free(line);
        return NULL;
    }
return line;
}

关于c - fprintf valgrind 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30899986/

相关文章:

c++ - Valgrind 未显示使用不正确的 c_str() 的无效内存访问

c - Macro 的类型转换以优化代码

c - 这段代码在 C 中意味着什么, "int x = ~!printf; "?

c - 在 C 中打印定义的整数

C 编程 - 释放一个用单引号填充的字符 ** 和用双引号填充的字符之间的区别 - valgrind

c - valgrind 使用 getline 函数报告内存丢失

c - 在 mac osx 上找不到 endian.h

在运行时定义的 C++ 全局 extern 常量可跨多个源文件使用

c - C linux查询MX记录

c - 在 C 中打印 unicode 字符串