This is a discussion on my good friend Mr Seg Fault within the C Programming forums, part of the General Programming Boards category; Originally Posted by std10093 Ok here is my approach Notice that i did not use realloc.When you think of an ...
Haha, thanks, but forgetting to free my memory is a big mistake! Bravo for your observation
So right before return 0; (where main terminates at these two lines of code)
Hmm.....Code:for(i = 0 ; i < answerCounter ; i++) free(answer[i]);
If you notice we make one pass to print and one pass to free....Thus we execute these loops at aggregate 20 times... If we had as size of array 1000 instead of 10, we would execute these two 2000 times...
So if we have as size of array answer the value n, we will do a pass of 2*n ... so do it in one pass to execute n times
And because this n is how many times our while loop is going to be executed, we have to consider the aboveCode:for(i = 0 ; i < answerCounter ; i++) { printf("%s\n",answer[i]); free(answer[i]); }
Remember that the time complexity of our algorithm is determined by the heaviest operation!