Lesson 20 of 24

Pointers, new/delete ಮತ್ತು Smart Pointers

11 min 35 XP

new heap memory ಕೊಡುತ್ತದೆ, delete ಅದನ್ನು ಬಿಡುಗಡೆ ಮಾಡುತ್ತದೆ; delete ಮರೆತರೆ memory leak. Modern C++ ತಾನಾಗಿಯೇ memory ಬಿಡುಗಡೆ ಮಾಡುವ unique_ptr ಮತ್ತು shared_ptr ಬಳಸುತ್ತದೆ.

Example: edit me and press Run

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

new[] ಜೊತೆ delete[], new ಜೊತೆ delete ಜೋಡಿಸಿ; ಅವನ್ನು ಬೆರೆಸುವುದು undefined behaviour. Smart pointers ಈ ತಪ್ಪುಗಳನ್ನು ಸಂಪೂರ್ಣ ತಪ್ಪಿಸುತ್ತವೆ.

Challenge

+35 XP

Singly linked list: n ಮತ್ತು n ಸಂಖ್ಯೆಗಳನ್ನು ಓದಿ, ಪ್ರತಿಯೊಂದನ್ನು list ನ head ನಲ್ಲಿ insert ಮಾಡಿ, ನಂತರ head ನಿಂದ list print ಮಾಡಿ. (Nodes ಗೆ unique_ptr ಅಥವಾ new ಬಳಸಿ.) Input: 4 1 2 3 4 Output: 4 3 2 1

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