-1
*** glibc detected *** ./main: corrupted double-linked list: 0x086c4f30 ***

After this the program does not exit and I am forced to exit using cntrl+C. I am not using any memory de allocation like "delete" in my whole code either

On using Valgrind, i get the following message:

Invalid write of size 4
==20358==    at 0x8049932: main (main.cpp:123)
==20358==  Address 0x432e6f8 is 0 bytes after a block of size 16 alloc'd
==20358==    at 0x402C454: operator new[](unsigned int) (in        /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==20358==    by 0x8049907: main (main.cpp:120)

And the corresponding piece of code in line 123 is

float **der_global= new float *[NODES];
for(int i=0; i<no_element; i++)
{
der_global[i]=new float [no_element];
}
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Karthik
  • 69
  • 6

2 Answers2

0

This error usually shows up when the program frees memory that is no longer valid. Are u using malloc or any other dynamic allocation.

It would be easy to solve ur problem if u could add some of your code

Try using valgrind

valgrind --tool=memcheck --leak-check=full --track-origins=yes --show-reachable=yes --log-file=val.log ./<executable> <parameters>

and look at the val.log

You could also use gdb but for that u will need to compile with the -g tag

  • I added the message I get from Valgrind – Karthik Dec 24 '13 at 12:32
  • try using explicit value in the size of new. instead of using NODES or no_element use 1 or 10 or something like that – Archit Khosla Dec 24 '13 at 12:36
  • Could you please help me. I don't know how to do it in another way – Karthik Dec 24 '13 at 12:38
  • your der_global is a double pointer and the new float should return just a pointer and if thats not the case then `float **der_global= new float *[NODES];` is wrong – Archit Khosla Dec 24 '13 at 12:38
  • [link](http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) suggested the way for me to allocate memory in the current fashion. DO you suggest that I do it in a linear array? – Karthik Dec 24 '13 at 12:43
  • you may want to read http://www.cplusplus.com/doc/tutorial/dynamic/ or try using a vector instead – Archit Khosla Dec 24 '13 at 12:45
0

Your original new call gives you space to store NODES pointers; but your for-loop tries to set no_element of them, which doesn't have to be the same number. Your for loop should have i less than NODES, not i less than no_element.

tabstop
  • 1,751
  • 11
  • 10