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