Lesson 20 of 24

Pointers, new/delete आणि Smart Pointers

11 min 35 XP

new heap memory देते आणि delete ती मोकळी करते; delete विसरलात तर memory leak होते. Modern C++ unique_ptr आणि shared_ptr वापरते, जे memory आपोआप मोकळी करतात.

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.