This is a discussion on How to check if an array of doubles is sorted? within the C Programming forums, part of the General Programming Boards category; Originally Posted by camel-man Why are you comparing to array[i-1] why not just i? You're also scanning back to back ...
Ahh ok, I thought he was trying to actually sort the array from beginning.
ah ok, right. So std10093, should I put free(array) before return 0? what if I dont put the free statement?
Yes, as well as one before return 1, or you could have a memory leak.
thanks, camel-man and std!
Well if you do not put it you may get a segmentation fault or a bus error (the 1st more likely) .If you are on windows your program may terminates safely but this is so bad,because this way you can not understand that you have a memory leak ,so you might not spot it and as a result not fixing it!!!
Why do we free our memory?
Imagine you have a LARGE program.Your program allocates sources.Sources of what?Of a system,let's say of your pc.As you can imagine the memory is limited in a computer (it is much but not infinite).So imagine that you had many calls of mallocs and you store big blocks of memory but you do not free them when you are done with them.Then you are in danger of running out of available memory!!So next you are going to allocate something,this will be impossible and malloc will return NULL.
This may be seem not so dangerous if you have a single main,but imagine a function that is called MANY times,allocates some memory for a usual local variable(which dies when the function terminates) and you do not free this memory when the function call ends.So you keep this memory allocated for no reason!Also then you have lost access to that memory.
So,always free your allocated memory when not needed anymore!