struggling with the last piece of the puzzle for Recover. I'm pretty sure It's related to a memory leak of sorts, but I fail to understand how to resolve it. Here is my code: http://pastebin.com/3jmAnZ8D

Here are my file sizes (as per curious kiwi in another thread: here)

-rw------- 1 jharvard students   151552 Oct  8 19:39 000.jpg
-rw------- 1 jharvard students   389120 Oct  8 19:39 001.jpg
-rw------- 1 jharvard students   196608 Oct  8 19:39 002.jpg
-rw------- 1 jharvard students    69632 Oct  8 19:39 003.jpg
-rw------- 1 jharvard students   163840 Oct  8 19:39 004.jpg
-rw------- 1 jharvard students   106496 Oct  8 19:39 005.jpg
-rw------- 1 jharvard students    90112 Oct  8 19:39 006.jpg
-rw------- 1 jharvard students   118784 Oct  8 19:39 007.jpg
-rw------- 1 jharvard students   167936 Oct  8 19:39 008.jpg
-rw------- 1 jharvard students   319488 Oct  8 19:39 009.jpg
-rw------- 1 jharvard students    81920 Oct  8 19:39 010.jpg
-rw------- 1 jharvard students   217088 Oct  8 19:39 011.jpg
-rw------- 1 jharvard students   307200 Oct  8 19:39 012.jpg
-rw------- 1 jharvard students   159744 Oct  8 19:39 013.jpg
-rw------- 1 jharvard students   143360 Oct  8 19:39 014.jpg
-rw------- 1 jharvard students   382464 Oct  8 19:39 015.jpg

Here is my CS50 Check. https://sandbox.cs50.net/checks/d9408dc7018548cf89e3be7125feb17f

I see segmentation fault in the reason for fail, but this does not happen when I run it on my machine? Any input would be greatly appreciated!


I think the issue is with your char* title. You declare it like this:

char* title;

Since you haven't initialized it (either to null, or to point to some meaningful place in memory, it points somewhere random, based on whatever garbage data was in the memory that now holds your title pointer. In my computer, it pointed to a long string of bytes as it was a while before it found any null terminator:

Breakpoint 1, main (argc=1, argv=0xbffff194) at joshrecover.c:30
30      int foundOne = 0;
(gdb) display title
1: title = 0xbffff19c "\215\363\377\277\230\363\377 etc.
(gdb) c

Breakpoint 2, main (argc=1, argv=0xbffff194) at joshrecover.c:76
76              sprintf(title,"%03d.jpg",count);
1: title = 0xbffff19c "\215\363\377\277\230\363\377 etc.
(gdb) n
78              fo = fopen(title, "a");
1: title = 0xbffff19c "000.jpg"

So you can see that line 76 has the effect of over-writing whatever memory happens to live in the 32 bytes unlucky enough to be pointed to by the uninitialized pointer. In your system and mine, we got away with it, but likely on the server, the uninitialized pointer happened to point to some memory that shouldn't be accessed.

Since you have called free() on title when your program terminates, you can probably just change your declaration to include a call to malloc() asking for the right amount of space, or alternatively, you can declare title as a char array with a given number of elements.

I hope this helps.

  • That was great. Thank you. I resolved it by initializing title to NULL, and then each time I find a jpg, I malloc at that time. and free when I close the file. Oct 11 '15 at 23:39

You must log in to answer this question.

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