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.