Ex. M3:06

38 views
Skip to first unread message

colorad...@gmail.com

unread,
Jul 8, 2016, 2:43:39 PM7/8/16
to mooc.fi
Hi,

I'm having problems with module 3-06_oodi

The code compiles fine and runs correctly. When I submit I get a Valgrind failure



*Explain your problem here in English or Finnish*


Best regards


Supported languages are English and Finnish

Please use our build in TMC Pastebin when asking questions related to your code  
 
TMC Plugin has built-in code sharing feature. 
In NetBeans, select TMC -> Send code to TMC pastebin. Copy the link you receive and paste it to your message.  

You may use our web interface to share code snippets: http://paste.mooc.fi

ljleppan

unread,
Jul 19, 2016, 4:37:21 AM7/19/16
to mooc.fi, colorad...@gmail.com
Hi,

Your program has some memory management issues. The Valgrind report is a bit hard to read, but can be deciphered :)

Let's first take a look at the top-most block:

==387== Invalid write of size 1
==387==    at 0x4C2C2B3: strcpy (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==387==    by 0x40313A: init_record (oodi.c:27)
==387==    by 0x401C24: test_init_record (test_source.c:73)
==387==    by 0x4068C7: srunner_run (in /tmc/test/test)
==387==    by 0x402D30: tmc_run_tests (tmc-check.c:122)
==387==    by 0x40273F: main (test_source.c:272)
==387== Address 0x590824a is 0 bytes after a block of size 10 alloc'd
==387==    at 0x4C28C20: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==387==    by 0x403118: init_record (oodi.c:26)
==387==    by 0x401C24: test_init_record (test_source.c:73)
==387==    by 0x4068C7: srunner_run (in /tmc/test/test)
==387==    by 0x402D30: tmc_run_tests (tmc-check.c:122)
==387==    by 0x40273F: main (test_source.c:272)

The first line (==387== Invalid write of size 1) is telling us that the our code is writing more data than we have space for. 

The next few lines are a stacktrace, telling us where the problem happened: the issue happened during a strcpy call, that started in the init_record function, located on the line 27 of the file oodi.c. The rest of the stacktrace seems irrelevant, since it's happening outside of the code we wrote.

The second block within the larger block (Address 0x590824a is 0 bytes after a block of size 10 alloc'd) tells us where the memory we are having trouble with was allocated: it was allocated using malloc on line 26 of the file oodi.c.

Looking at those lines, we see that the strcpy is doing what we want it to do, so that's not the problem. Therefore the problem must be that we are allocating too little memory. Here, the issue is that strlen does not include the string terminating null byte in the string length, but strcpy still copies that byte. So your malloc is not allocating the necessary memory to hold the null byte and it overflows, causing Valgrind to throw a fit.

You should be able to figure out the other problems as well using a similar process. Often it happens that fixing a single wrongly sized malloc removes many of those errors :)

-L
Reply all
Reply to author
Forward
0 new messages