I’m passing a dynamic allocated variables into a function , and after returning from that
function , when I run inputs on the code , some of those inputs makes the program to crash .
As you can see , I test the argv array , twice , once at the end of divide , and another
time after I return from divide . In the first check I get all the values of argv from
index 0 to argc , but in the 2nd check (after returning from divide back into main)
index 0 make the for loop to crash .
So obviously I’m doing something wrong , any idea what ?
This is the likely culprit. Calling
realloc()can allocate a new (presumably larger) block of memory into which the data from the old block is copied, and then deallocate the old block. The problem isn’t that you’re changing the value ofargvinmain(), it’s that you’re not changing it. You’re deallocating the block that it points to without updating main()’sargvto point to the new block.