[{"data":1,"prerenderedAt":186},["ShallowReactive",2],{"lesson-ownership":3,"lesson-navigation":165},{"id":4,"title":5,"body":6,"description":130,"extension":131,"label":132,"lessonId":133,"meta":134,"minutes":135,"navigation":136,"order":137,"path":138,"quiz":139,"seo":162,"slug":133,"stem":163,"__hash__":164},"lessons\u002Flessons\u002Fownership.md","Someone has to clean up.",{"type":7,"value":8,"toc":125},"minimark",[9,14,18,25,67,74,78,89,96,100,106,111,121],[10,11,13],"h2",{"id":12},"the-mess","The mess",[15,16,17],"p",{},"Your AI leaves a “remember to free this” comment. Adorable. Comments are famously terrible garbage collectors.",[15,19,20,24],{},[21,22,23],"strong",{},"Ownership means responsibility for cleanup."," Think of a borrowed apartment key: knowing the address doesn't make you the landlord. A raw pointer stores an address; it doesn't tell you who must release the resource.",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-cpp shiki shiki-themes github-light github-dark","void process() {\n    auto* buffer = new int[64];\n    if (!ready()) return; \u002F\u002F The comment will clean up. Surely.\n    delete[] buffer;\n}\n","cpp","",[33,34,35,43,49,55,61],"code",{"__ignoreMap":31},[36,37,40],"span",{"class":38,"line":39},"line",1,[36,41,42],{},"void process() {\n",[36,44,46],{"class":38,"line":45},2,[36,47,48],{},"    auto* buffer = new int[64];\n",[36,50,52],{"class":38,"line":51},3,[36,53,54],{},"    if (!ready()) return; \u002F\u002F The comment will clean up. Surely.\n",[36,56,58],{"class":38,"line":57},4,[36,59,60],{},"    delete[] buffer;\n",[36,62,64],{"class":38,"line":63},5,[36,65,66],{},"}\n",[15,68,69,70,73],{},"If ",[33,71,72],{},"ready()"," returns false, the pointer disappears and the allocation stays. You've invented a donation scheme for RAM.",[10,75,77],{"id":76},"the-fix","The fix",[15,79,80,81,84,85,88],{},"Use ",[33,82,83],{},"std::vector\u003Cint>"," for this buffer: it owns its storage and releases it on destruction. For a single separately owned object, consider ",[33,86,87],{},"std::unique_ptr",".",[15,90,91,92,95],{},"Several functions can borrow an object without owning it. Use ",[33,93,94],{},"std::shared_ptr"," only for actual shared ownership; strong reference cycles can prevent cleanup. “Shared everywhere” is a custody dispute, not a design.",[10,97,99],{"id":98},"ask-your-ai","Ask your AI",[101,102,103],"blockquote",{},[15,104,105],{},"Name each resource's owner. Show cleanup on success, early return, and exceptions. Use an owning type; justify any shared ownership.",[15,107,108],{},[21,109,110],{},"No named owner? No convincing cleanup story. Reject the hand-waving.",[15,112,113,114,88],{},"Receipts: ",[115,116,120],"a",{"href":117,"rel":118},"https:\u002F\u002Fisocpp.github.io\u002FCppCoreGuidelines\u002FCppCoreGuidelines#S-resource",[119],"nofollow","C++ Core Guidelines — resource management",[122,123,124],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":31,"searchDepth":45,"depth":45,"links":126},[127,128,129],{"id":12,"depth":45,"text":13},{"id":76,"depth":45,"text":77},{"id":98,"depth":45,"text":99},"Your AI allocated it. Your comment won’t free it. Someone needs an actual job.","md","OWNERSHIP","ownership",{},"2",true,"1","\u002Flessons\u002Fownership",[140,147,155],{"prompt":141,"options":142,"answer":39,"explanation":146},"Two raw pointers, same address. How many owners does that prove?",[143,144,145],"Two: each pointer owns the target.","You cannot infer ownership from addresses.","One: whichever pointer was created first.","An address shows where, not who cleans up. Owning types express responsibility. Pointer counting is not an ownership model.",{"prompt":148,"options":149,"answer":153,"explanation":154},"An early return skips manual cleanup. What needs fixing?",[150,151,152],"Give the resource an owner that handles every ordinary exit.","Add cleanup after the return.","Give the raw pointer a shorter scope.",0,"An owning object cleans up on destruction. An unreachable delete is just decorative punctuation.",{"prompt":156,"options":157,"answer":45,"explanation":161},"The AI uses shared_ptr everywhere. Problem solved?",[158,159,160],"Yes: shared pointers cannot leak.","Yes: every borrower needs shared ownership.","No: shared ownership needs a reason; strong cycles can leak.","Borrowing needs no ownership share. Strong cycles can prevent cleanup. You made a reference-counted group project.",{"title":5,"description":130},"lessons\u002Fownership","0j5YP6N0LZBF_HC7Qj5WlSH_CVojrQcAltFCbxygVlo",[166,167,170,174,178,182],{"lessonId":133,"slug":133,"title":5,"order":137},{"lessonId":168,"slug":168,"title":169,"order":135},"lifetime","Your pointer outlived its welcome.",{"lessonId":171,"slug":171,"title":172,"order":173},"references-and-pointers","Borrowed doesn’t mean immortal.","3",{"lessonId":175,"slug":175,"title":176,"order":177},"raii","Make cleanup someone’s actual job.","4",{"lessonId":179,"slug":179,"title":180,"order":181},"copies-and-moves","Moving is not a magic speed spell.","5",{"lessonId":183,"slug":183,"title":184,"order":185},"undefined-behavior","“It worked” is not a warranty.","6",1789406997108]