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

  1. build program debug info (most -g flag). let valgrind tell exactly line triggers invalid read , write
  2. if problem doesn't become obvious, edit question , include entire valgrind output.
  3. 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

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -