[{"data":1,"prerenderedAt":191},["ShallowReactive",2],{"lesson-references-and-pointers":3,"lesson-navigation":170},{"id":4,"title":5,"body":6,"description":135,"extension":136,"label":137,"lessonId":138,"meta":139,"minutes":140,"navigation":141,"order":142,"path":143,"quiz":144,"seo":167,"slug":138,"stem":168,"__hash__":169},"lessons\u002Flessons\u002Freferences-and-pointers.md","Borrowed doesn’t mean immortal.",{"type":7,"value":8,"toc":130},"minimark",[9,14,18,34,70,73,77,84,94,99,103,109,126],[10,11,13],"h2",{"id":12},"the-mess","The mess",[15,16,17],"p",{},"Your AI replaces copies with borrowed access. “Optimized.” Yes, accessing dead storage is wonderfully cheap if correctness isn't a requirement.",[15,19,20,21,25,26,29,30,33],{},"A ",[22,23,24],"strong",{},"reference"," aliases an object; a ",[22,27,28],{},"pointer"," stores an address. A ",[22,31,32],{},"view"," borrows data. None automatically keeps its target alive. Having someone's address doesn't stop them moving.",[35,36,41],"pre",{"className":37,"code":38,"language":39,"meta":40,"style":40},"language-cpp shiki shiki-themes github-light github-dark","std::vector\u003Cint> scores{10};\nint* first = &scores[0];\nscores.reserve(scores.capacity() + 1); \u002F\u002F New storage, if successful.\nstd::cout \u003C\u003C *first;                  \u002F\u002F Visiting the demolished house.\n","cpp","",[42,43,44,52,58,64],"code",{"__ignoreMap":40},[45,46,49],"span",{"class":47,"line":48},"line",1,[45,50,51],{},"std::vector\u003Cint> scores{10};\n",[45,53,55],{"class":47,"line":54},2,[45,56,57],{},"int* first = &scores[0];\n",[45,59,61],{"class":47,"line":60},3,[45,62,63],{},"scores.reserve(scores.capacity() + 1); \u002F\u002F New storage, if successful.\n",[45,65,67],{"class":47,"line":66},4,[45,68,69],{},"std::cout \u003C\u003C *first;                  \u002F\u002F Visiting the demolished house.\n",[15,71,72],{},"Assume the requested capacity is representable. Successful reallocation invalidates pointers and references to the old elements. The vector survives. Your pointer's usefulness doesn't.",[10,74,76],{"id":75},"the-fix","The fix",[15,78,79,80,83],{},"Obtain the element again ",[22,81,82],{},"after"," reallocation, checking that the index is valid. Or prevent invalidation while borrowing.",[15,85,20,86,89,90,93],{},[42,87,88],{},"std::string_view"," also needs its backing characters to remain alive and valid. ",[42,91,92],{},"const"," restricts modification through an access path; it isn't life insurance.",[15,95,96],{},[22,97,98],{},"Check both lifetime and invalidation. “The container still exists” is half a defense.",[10,100,102],{"id":101},"ask-your-ai","Ask your AI",[104,105,106],"blockquote",{},[15,107,108],{},"Who owns this borrowed data? What destroys or invalidates it? Fix every use that crosses either event, citing the container's guarantees.",[15,110,111,112,119,120,125],{},"Receipts: ",[113,114,118],"a",{"href":115,"rel":116},"https:\u002F\u002Feel.is\u002Fc++draft\u002Fvector.capacity",[117],"nofollow","Vector reallocation"," · ",[113,121,124],{"href":122,"rel":123},"https:\u002F\u002Feel.is\u002Fc++draft\u002Fstring.view.template",[117],"String views",".",[127,128,129],"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":40,"searchDepth":54,"depth":54,"links":131},[132,133,134],{"id":12,"depth":54,"text":13},{"id":75,"depth":54,"text":76},{"id":101,"depth":54,"text":102},"“Zero-copy.” Also zero guarantees that the thing you borrowed still exists. Nice optimization.","md","REFERENCES & POINTERS","references-and-pointers",{},"2",true,"3","\u002Flessons\u002Freferences-and-pointers",[145,153,160],{"prompt":146,"options":147,"answer":151,"explanation":152},"Does string_view keep its backing string alive?",[148,149,150],"No: the characters must stay alive and valid separately.","Yes: it shares ownership.","Only if the view is const.",0,"A view borrows characters. It owns precisely none of them. Budget accordingly.",{"prompt":154,"options":155,"answer":48,"explanation":159},"A vector reallocates. Can you use an old element pointer?",[156,157,158],"Yes: the vector itself still exists.","No: reallocation invalidated it.","Yes: the element value is unchanged.","The vector replaced its storage. Same container, dead address. Your pointer missed the moving announcement.",{"prompt":161,"options":162,"answer":54,"explanation":166},"What lifetime guarantee does const access provide by itself?",[163,164,165],"The target lives as long as the reference.","The owner cannot destroy the target.","None: restricted mutation is not lifetime management.","Const restricts modification through that access. It is not cryogenic storage. Specific temporary-lifetime rules are a separate topic.",{"title":5,"description":135},"lessons\u002Freferences-and-pointers","KBhQ4Tb6NSSvwpoEXGEDnAHBEcxhg7X2ZnVdkSqgz9k",[171,175,178,179,183,187],{"lessonId":172,"slug":172,"title":173,"order":174},"ownership","Someone has to clean up.","1",{"lessonId":176,"slug":176,"title":177,"order":140},"lifetime","Your pointer outlived its welcome.",{"lessonId":138,"slug":138,"title":5,"order":142},{"lessonId":180,"slug":180,"title":181,"order":182},"raii","Make cleanup someone’s actual job.","4",{"lessonId":184,"slug":184,"title":185,"order":186},"copies-and-moves","Moving is not a magic speed spell.","5",{"lessonId":188,"slug":188,"title":189,"order":190},"undefined-behavior","“It worked” is not a warranty.","6",1789406997109]