[{"data":1,"prerenderedAt":183},["ShallowReactive",2],{"lesson-lifetime":3,"lesson-navigation":161},{"id":4,"title":5,"body":6,"description":127,"extension":128,"label":129,"lessonId":130,"meta":131,"minutes":132,"navigation":133,"order":132,"path":134,"quiz":135,"seo":158,"slug":130,"stem":159,"__hash__":160},"lessons\u002Flessons\u002Flifetime.md","Your pointer outlived its welcome.",{"type":7,"value":8,"toc":122},"minimark",[9,14,18,25,61,71,75,82,85,91,95,101,118],[10,11,13],"h2",{"id":12},"the-mess","The mess",[15,16,17],"p",{},"The AI returns a pointer to a local variable. You check that it isn't null. Excellent. You've confirmed the corpse has an address.",[15,19,20,24],{},[21,22,23],"strong",{},"Lifetime is how long an object exists."," Your hotel room number survives checkout. Your booking doesn't. A pointer is the room number, not a lifetime extension.",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-cpp shiki shiki-themes github-light github-dark","int* getAnswer() {\n    int answer = 42;\n    return &answer; \u002F\u002F Forwarding address for a dead local.\n}\n","cpp","",[33,34,35,43,49,55],"code",{"__ignoreMap":31},[36,37,40],"span",{"class":38,"line":39},"line",1,[36,41,42],{},"int* getAnswer() {\n",[36,44,46],{"class":38,"line":45},2,[36,47,48],{},"    int answer = 42;\n",[36,50,52],{"class":38,"line":51},3,[36,53,54],{},"    return &answer; \u002F\u002F Forwarding address for a dead local.\n",[36,56,58],{"class":38,"line":57},4,[36,59,60],{},"}\n",[15,62,63,66,67,70],{},[33,64,65],{},"answer"," dies when the function returns. Reading through that pointer afterward is ",[21,68,69],{},"undefined behavior",": C++ promises no particular result. A successful demo doesn't resurrect anything.",[10,72,74],{"id":73},"the-fix","The fix",[15,76,77,78,81],{},"Return this integer ",[21,79,80],{},"by value",". The caller gets a value instead of depending on a dead local.",[15,83,84],{},"The same trap catches callbacks that borrow locals and run later. Give the callback data it owns, where appropriate, or otherwise keep its target alive through every use.",[15,86,87,90],{},[21,88,89],{},"Non-null doesn't mean alive."," Stop asking a null check to perform a séance.",[10,92,94],{"id":93},"ask-your-ai","Ask your AI",[96,97,98],"blockquote",{},[15,99,100],{},"When does this target die? Can any returned pointer or delayed callback outlive it? Fix the lifetime and explain why every use stays valid.",[15,102,103,104,111,112,117],{},"Receipts: ",[105,106,110],"a",{"href":107,"rel":108},"https:\u002F\u002Feel.is\u002Fc++draft\u002Fbasic.life",[109],"nofollow","Object lifetime"," · ",[105,113,116],{"href":114,"rel":115},"https:\u002F\u002Feel.is\u002Fc++draft\u002Fstmt.return",[109],"Return statements",".",[119,120,121],"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":123},[124,125,126],{"id":12,"depth":45,"text":13},{"id":73,"depth":45,"text":74},{"id":93,"depth":45,"text":94},"The object died. Your pointer kept the address. Very thoughtful. Completely useless.","md","LIFETIME","lifetime",{},"2",true,"\u002Flessons\u002Flifetime",[136,143,150],{"prompt":137,"options":138,"answer":45,"explanation":142},"Read through a returned pointer to a dead local. What happens?",[139,140,141],"The pointer keeps the local alive.","The old value is guaranteed until the next call.","Undefined behavior: the target no longer exists.","Returning an address does not extend this lifetime. The variable died. Your confidence attended neither the funeral nor the review.",{"prompt":144,"options":145,"answer":39,"explanation":149},"A pointer passes a null check. What have you proved?",[146,147,148],"Its target is alive.","Only that the pointer is not null.","It is safe to dereference once.","Dangling pointers can be non-null. Congratulations: you checked the address, not whether anyone lives there.",{"prompt":151,"options":152,"answer":156,"explanation":157},"A callback borrows a local and runs after it dies. The fix?",[153,154,155],"Own suitable data or otherwise keep the target alive.","Make the borrowed reference const.","Check the pointer before scheduling the callback.",0,"The target must survive every access. Owning a suitable copy can help. Running later does not make death negotiable.",{"title":5,"description":127},"lessons\u002Flifetime","62ycXtxO89d0F1gj62Efq7PAwUgNV70p2wQnlENuM0g",[162,166,167,171,175,179],{"lessonId":163,"slug":163,"title":164,"order":165},"ownership","Someone has to clean up.","1",{"lessonId":130,"slug":130,"title":5,"order":132},{"lessonId":168,"slug":168,"title":169,"order":170},"references-and-pointers","Borrowed doesn’t mean immortal.","3",{"lessonId":172,"slug":172,"title":173,"order":174},"raii","Make cleanup someone’s actual job.","4",{"lessonId":176,"slug":176,"title":177,"order":178},"copies-and-moves","Moving is not a magic speed spell.","5",{"lessonId":180,"slug":180,"title":181,"order":182},"undefined-behavior","“It worked” is not a warranty.","6",1789406997108]