0
的无效读我不能找到在这个代码中的错误,林看着它几个小时... Valgrind的说:Valgrind的错误:大小1
==23114== Invalid read of size 1
==23114== Invalid write of size 1
我试图调试一些printfs输出,并且我认为错误在这个函数中。
void rdm_hide(char *name, Byte* img, Byte* bits, int msg, int n, int size)
{
FILE *fp;
int r;/
Byte* used;
int i = 0, j = 0;
int p;
fp = fopen(name, "wb");
used = malloc(sizeof(Byte) * msg);
for(i = 0; i < msg; i++)
used[i] = -1;
while(i < 3)
{
if(img[j] == '\n')
i++;
j++;
}
for(i = 0; i < msg; i++)
{
r = genrand_int32();
p = r % n;
if(!search(p, used, msg))
{
used[i] = (Byte)p;
if(bits[i] == (Byte)0)
img[j + p] = img[j + p] & (~1);
else if(bits[i] == (Byte)1)
img[j + p] = img[j + p] | 1;
}
else
i --;
}
for(i = 0; i < size; i++)
fputc((char) img[i], fp);
fclose(fp);
free(used);
}
感谢您的帮助!
你可以运行valgrind选项** - read-var-info = yes **并共享输出? – tuxuday
当你正在写入和读取img指向的数组时,它可能正在发生。使用gdb来查明它正在发生什么行。 – sj755