Lesson 21 of 23
Dynamic Memory: malloc and free
10 min 35 XP
When the size is only known at run time, ask for heap memory with malloc. Release it with free when you're done, or you leak memory.
Example: edit me and press Run
Output Loading C (first time takes a few seconds)
Press Run to see the result here.malloc vs calloc: calloc also zeroes the memory; realloc resizes it. Stack vs heap is a frequent interview question.