I am facing a problem where I get double free or corruption error when I try to free up a memory, outside of the function in which I had allocated that block of memory, even though I had passed the pointer to the memory block to a pointer outside of that function.
My code is such that in the main()
function, I make a call to a function with the definition char * reverseComplement(char * pattern);
as such:
char * rev = reverseComplement(dna_input);
where dna_input
is a char
pointer to a block of allocated memory that was allocated within main()
. Within the reverseComplement()
function, there is a line where I allocate memory and then return the pointer to that block of memory at the very end.
...
...
char * revcomplpattern = (char *)malloc(strlen(pattern));
...
...
return revcomplpattern;
One reason I thought could be causing the problem is that after the reverseComplement()
function finished executing, its stack gets torn down so I would lose the access to the memory on the heap. But this shouldn't be because I had passed over the handle of the heap-allocated memory over from revcomplpattern
which was residing in reverseComplement()
over to rev
which is residing in main()
. So free(rev)
in main()
should do the job.
I don't know what I might be doing wrong here, and any help is greatly appreciated!