【发布时间】:2014-11-17 07:21:09
【问题描述】:
我有一个程序(处于初级阶段)应该从标准输入获取文件,对其进行编码并将其打印到标准输出。无论我读入什么类型的标准输入,它似乎都可以正常工作。但是,Valgrind 告诉我有一个问题:
==6508== Invalid read of size 4
==6508== at 0x4009EC: insertObject (lzw.c:106)
==6508== by 0x400B72: lzw_encode (lzw.c:228)
==6508== by 0x40091D: main (lzw.c:48)
==6508== Address 0x5203044 is 0 bytes after a block of size 65,540 alloc'd
==6508== at 0x4C2845D: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==6508== by 0x400D51: initialize (lzw.c:305)
==6508== by 0x400AB3: lzw_encode (lzw.c:196)
==6508== by 0x40091D: main (lzw.c:48)
==6508==
以下是相关的sn-ps代码:
int lzw_encode ()
{
table* hashtable;
int counter=0;
hashtable = initialize(); //line 196
int code = -1;
char k;
if ((k=getc(stdin))==EOF)
{
return 0;
}
code = k;
while ((k=getc(stdin))!=EOF)
{
if (HashSearch(hashtable, code, k)!=-1)
{
code=HashSearch(hashtable, code, k);
}
else
{
putBits(12, code);
if (hash(code, k, hashtable->size)>4340)
{
counter=1;
}
if (counter==0)
{
hashtable = insertObject(hashtable, code, k); //line 228
}
code = HashSearch(hashtable, -1, k);
}
}
if (code != 0)
{
printf ("%d\n", code);
}
/*if (!fp)
{
printf("????\n");
}*/
return 0;
}
插入对象:
table *insertObject (table *h, int pref, char ch)
{
struct node x;
int i;
if (ch < 0)
{
ch=4096-ch;
}
x.chr=ch;
x.pref=pref;
i = hash(pref, ch, h->size);
while (h->hash[i].pref!=0)
{
i++;
}
if (i>4340)
{
return h;
}
h->hash[i]=x; //line 106
return h;
}
初始化:
table *initialize ()
{
table *hashtable = malloc(sizeof(table)); //Line 305
//hashtable->hash = malloc (sizeof(struct node) * 4096);
memset(hashtable, 0, sizeof(*hashtable));
hashtable->size=4096;
//hashtable->hash=malloc(sizeof(table));
for (int i=0; i<4096; i++)
{
hashtable->hash[i].pref=-1;
hashtable->hash[i].before=-1;
hashtable->hash[i].after=-1;
hashtable->hash[i].chr=0;
}
for (int i=0; i<256; i++)
{
hashtable->hash[i].chr=i+128;
}
//printf("Initialized\n");
return hashtable;
}
我已经标记了 valgrind 中提到的行。
编辑:
以下是数据结构:
typedef struct hash_t table;
struct node {
int pref;
int before;
int after;
char chr;
};
struct hash_t {
int size;
struct node hash[4096];
};
EDIT2:确保我永远不会超过 4096 似乎已修复它。但是,我发现了一个我以前见过的 valgrind 错误:
==20137== Syscall param write(buf) points to uninitialised byte(s)
==20137== at 0x4F198B0: __write_nocancel (in /usr/lib64/libc-2.17.so)
==20137== by 0x4EA8E32: _IO_file_write@@GLIBC_2.2.5 (in /usr/lib64/libc-2.17.so)
==20137== by 0x4EAA29B: _IO_do_write@@GLIBC_2.2.5 (in /usr/lib64/libc-2.17.so)
==20137== by 0x4EABDE6: _IO_flush_all_lockp (in /usr/lib64/libc-2.17.so)
==20137== by 0x4EABF39: _IO_cleanup (in /usr/lib64/libc-2.17.so)
==20137== by 0x4E6AE0A: __run_exit_handlers (in /usr/lib64/libc-2.17.so)
==20137== by 0x4E6AEA4: exit (in /usr/lib64/libc-2.17.so)
==20137== by 0x4E53AFB: (below main) (in /usr/lib64/libc-2.17.so)
==20137== Address 0x4023002 is not stack'd, malloc'd or (recently) free'd
根据本网站上的其他问题,这似乎是在未初始化结构时发生的。但是,我没有看到任何未初始化的结构。这段代码有这样的问题吗,还是我应该在程序的其他地方看看?
【问题讨论】:
-
第一个想法:Valgrind 是对的。您的程序当前正在运行并不意味着它没有错误。
-
请告诉我们第 48 行。
-
保持一致,使用
malloc(sizeof(*hashtable)),或calloc(3) -
第48行是“return lzw_encode();”
-
当你的
i索引数组元素从0到4095时,它不应该大于4340,不是吗?