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 बनाम calloc: calloc memory को शून्य भी करता है; realloc उसका size बदलता है। Stack बनाम 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.