View Single Post
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#20
Originally Posted by omeriko9 View Post
I see... so this is a case where I should have used "malloc" & "free"... or use an allocated memory. Right?
Most likely, yes. Depending on how you get the string, you get three distinct cases:

You allocate the memory yourself using malloc. You need to use free to release it when it's no longer needed or your application will have a memory leak.

You allocate the memory on the stack, as in the fix above, and the memory will be deallocated as soon as you leave that specific stack scope/frame.

You get a pointer to statically, or otherwise, allocated memory that you should not try to deallocate when you're done with it. This is, from time to time, the most tricky case since you need to either know what to do or read the documentation carefully.
 

The Following User Says Thank You to Joorin For This Useful Post: