2.2.2 : Using all the hints and clues



The first hint we have is :



*** Error in `XXX/build/hadamard_product': double free or corruption (!prev): 0x0000000000615030 ***


That means we wrote too much values in a table and we overflow the size of this pointer.

We also know, the bug hapens at teh very first call of the function evaluateHadamardProduct, thank to the value of nbElement :



(gdb) print nbElement 
$1 = 1000


And finally, the bug is caused by the line 54 of our main.cpp program. This implies, the mistakes takes place before this line.

The coded line before is the line 51, with :



1
cout << "tabResult[0] = " << tabResult[0] << endl;


We are not expecting a bug here because the number of elements of the table tabResult is 1000 so tabResult[0] exists.

The coded line before is the line 49, with :



1
hadamard_product(tabResult, tabX, tabY, 4000lu);


Of course, there is a mistake here because 4000lu is greater than the expected 1000 elements, and the overflow is here.

So this line must be changed into :



1
hadamard_product(tabResult, tabX, tabY, nbElement);


Then we recompile :

1
2
3
4
5
make
Scanning dependencies of target hadamard_product
[ 50%] Building CXX object CMakeFiles/hadamard_product.dir/main.cpp.o
[100%] Linking CXX executable hadamard_product
[100%] Built target hadamard_product


And the execution is good :

1
2
3
4
5
6
7
./hadamard_product 
Hadamard product
tabResult[0] = 0
tabResult[0] = 0
tabResult[0] = 0
tabResult[0] = 0
tabResult[0] = 0


Do not forget to change the compilation options if you want to keep optimisation and to have a fast execution (without trouble this time).


1
add_definitions(-O3)