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.