still stuck on recover.

Valgrind is saying I have an uninitialized value when I call fwrite. I'm not sure why. I've read through all the segfault issues on here and can't seem to find one that references fwrite.

So I've got

BYTE s[512] as a 'buffer' fread(s,512 * sizeof(BYTE), 1, ptr) fwrite(s,512 * sizeof(BYTE), 1, jpg_file_ptr)

I get a segfault and Valgrind helpfully suggests I have unitialized values of size 8:

==2963== Use of uninitialised value of size 8

==2963== at 0x51ABDA0: fwrite (iofwrite.c:41)

==2963== by 0x4009E2: main (recover.c:84)


==2963== Use of uninitialised value of size 8

==2963== at 0x51ABDAF: fwrite (iofwrite.c:41)

==2963== by 0x4009E2: main (recover.c:84)

==2963== ==2963== Invalid read of size 8

==2963== at 0x51ABDBF: fwrite (iofwrite.c:41)

==2963== by 0x4009E2: main (recover.c:84)

==2963== Address 0x8 is not stack'd, malloc'd or (recently) free'd

Can someone explain why? I've gone through pointers and file I/O again and am still stuck. Basically frustrated to still be stuck on this problem 3 days later...

Code here (while problem is open): http://pastebin.com/ADLZvbbx

(Bottom code can be ignored -- previous approach)

Thanks all!


The code tries to write out the first block of data before the first output file has been opened. You should check to make sure the output file is opened before attempting the write.

Once that's fixed, the code has a problem handling the first file. It will open and immediately close the first file, then open the second one before it writes any data out.

Finally, you're using two different pointers for the input file in order to detect EOF. Unfortunately, this approach won't work. One is actually controlling the read while the other is trying to detect the EOF. They don't track together. IF they did, they'd be the same pointer and the fgets would throw the reads off of the 512 byte boundaries. In any case, this causes an infinite loop when the EOF is crossed because there's nothing to terminate the loop. You should consider putting the fread into the while loop setup and using the return value to detect EOF.

There may be more issues, but this will get the ball rolling again.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Miracle man, Mr. Cliff. Thanks! – borker Jul 10 '16 at 1:16
  • Fixed it and it is working, but getting a lot of errors on check50 FYI: ~/workspace/pset4/jpg $ check50 2015.fall.pset4.recover recover.c Checking........................... E_UNKNOWN error. – borker Jul 10 '16 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .