Solution:
The issue is with this code:
parent.left = &a;
parent.right = &b;
This is getting pointers to local variables, which will be reinitialized next time on all direction the loop. CharCountNode
will al last attempt to delete
these objects, however they haven't been allocated by new.
You require to make left
and right
point to objects allocated on the heap, as that is what CharCountNode
is expecting. Something like:
parent.left = new CharCountNode(a);
parent.right = new CharCountNode(b);
You require to include this to just earlier on return dataReturn;
in your dataTest
function:
dataReturn[i] = NULL ;
Else your while (data[i]) {}
will continue further than wanted.
And instead of:
dataReturn[i] = malloc( sizeof(char) * (strlen(*str)) );
write:
dataReturn[i] = malloc(strlen(*str) + 1);
With a view to allocating space for the terminating zero.
BTW sizeof (char)
is always 1.
python 3.6 malloc: pointer being freed was not allocated / set a breakpoint in malloc_error_break to debug Abort trap: 6