Lesson 21 of 23

Dynamic Memory: malloc, free

10 min 35 XP

Size run time-ൽ മാത്രം അറിയുമ്പോൾ malloc ഉപയോഗിച്ച് heap memory ചോദിക്കൂ. കഴിഞ്ഞാൽ free ഉപയോഗിച്ച് വിട്ടുകൊടുക്കൂ, അല്ലെങ്കിൽ memory leak ഉണ്ടാകും.

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 memory പൂജ്യമാക്കുകയും ചെയ്യുന്നു; realloc അതിന്റെ size മാറ്റുന്നു. Stack vs heap പതിവായി വരുന്ന interview ചോദ്യമാണ്.

Challenge

+35 XP

n വായിച്ച്, malloc ഉപയോഗിച്ച് n ints-ന്റെ array ഉണ്ടാക്കി, ആദ്യ n Fibonacci സംഖ്യകൾ (0, 1, 1, 2, ...) കൊണ്ട് നിറച്ച്, spaces കൊണ്ട് വേർതിരിച്ച് print ചെയ്ത്, പിന്നെ free ചെയ്യൂ. Input: 7 → Output: 0 1 1 2 3 5 8

Output Loading C (first time takes a few seconds)
Press Run to see the result here.