pointers - Valgrind error: Invalid read of size 1 -
i cant find error in code, im looking @ hours... valgrind says:
==23114== invalid read of size 1 ==23114== invalid write of size 1
i tried debugging printfs, , think error in function.
void rdm_hide(char *name, byte* img, byte* bits, int msg, int n, int size) { file *fp; int r;/ byte* used; int = 0, j = 0; int p; fp = fopen(name, "wb"); used = malloc(sizeof(byte) * msg); for(i = 0; < msg; i++) used[i] = -1; while(i < 3) { if(img[j] == '\n') i++; j++; } for(i = 0; < 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 --; } for(i = 0; < size; i++) fputc( (char) img[i], fp); fclose(fp); free(used); }
thanks help!
==23114== invalid read of size 1
==23114== invalid write of size 1
i pretty sure that's not all valgrind says.
you should
- build program debug info (most
-g
flag). let valgrind tell exactly line triggers invalid read , write - if problem doesn't become obvious, edit question , include entire valgrind output.
- re-running
valgrind --track-origins=yes your-exe
may provide additional useful info.
lastly, algorithm appears totally bogus. far can tell, j
becomes 3 after first while
loop , never changes after (in case should use const int j = 3;
, away j++
). also, reference img[j + p]
, p
between 0
, n
. if n
indeed size of img
, it's little surprise j + p
indexes outside of img
limits, , triggers both errors.
Comments
Post a Comment