Freeing at the end of the function can waste memory,and the code can let a object's life go out early object's life ,like #6
Freeing at the end of the function can waste memory,and the code can let a object's life go out early object's life ,like #6
> My mind is all malloc variables must free at the end of function can waste memory.
Then how would you deal with say strdup(), which makes a copy of a string in allocated memory, and returns the pointer to you.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I'm really confused as to how an easy to follow guideline has become a rule from me that we must follow and debate.My mind is all malloc variables must free at the end of function can waste memory.
Now imagine the angle that you made a small function:1 Rewrite code,first strlen then malloc then MYstrdup(stringpointer,mallocpointer,length) then use then free,
2 In some angle look strdup same as malloc( strlen( stringpointer ) ) to keep the code "style",
1) call strdup
2) do something to the string
3) print the result
4) free
It's a pretty easy guideline to understand how to use. I never promised it would work in every situation.
Is it just me, or does this image fail to communicate anything at all? I can't understand this.
You're very wrong, sorry.
Swaps do not at any point release memory, only a call to free would. If you have two pointers A and B, and you swap them, all that has happened is B points to what A used to point to, and A points to what B used to point to.
Swap can not free,it let free very very quickly and look clear,show in the hello world print program at #1
All you're saying is that it's faster.it let free very very quickly
You could waste your life trying to prove that your program is faster by measuring the time it takes your program to complete, and comparing it to the time of Salem's code. Then you would have some evidence of your claim.
Or you could stop now and realize that there is no important time difference between you two. The swap is nothing but an unnecessary step. I think if you actually measured where your program spent most of its time, you would see that the program can only be as fast as the time it takes printf to execute.
Well your first post fails to be clear on so many levels.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.