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.