[{"data":1,"prerenderedAt":179},["ShallowReactive",2],{"lesson-undefined-behavior":3,"lesson-navigation":158},{"id":4,"title":5,"body":6,"description":123,"extension":124,"label":125,"lessonId":126,"meta":127,"minutes":128,"navigation":129,"order":130,"path":131,"quiz":132,"seo":155,"slug":126,"stem":156,"__hash__":157},"lessons\u002Flessons\u002Fundefined-behavior.md","“It worked” is not a warranty.",{"type":7,"value":8,"toc":118},"minimark",[9,14,18,25,49,60,64,71,74,77,81,87,92,114],[10,11,13],"h2",{"id":12},"the-mess","The mess",[15,16,17],"p",{},"“It worked on my machine.” Lovely. So does a smoke alarm with the battery removed. Very quiet. Excellent reviews.",[15,19,20,24],{},[21,22,23],"strong",{},"Undefined behavior means C++ imposes no requirements for that behavior."," A crash isn't guaranteed. Neither is your preferred answer. Your successful demo isn't a contract.",[26,27,32],"pre",{"className":28,"code":29,"language":30,"meta":31,"style":31},"language-cpp shiki shiki-themes github-light github-dark","int count = std::numeric_limits\u003Cint>::max();\nint next = count + 1; \u002F\u002F Signed overflow. Optimism is not a larger type.\n","cpp","",[33,34,35,43],"code",{"__ignoreMap":31},[36,37,40],"span",{"class":38,"line":39},"line",1,[36,41,42],{},"int count = std::numeric_limits\u003Cint>::max();\n",[36,44,46],{"class":38,"line":45},2,[36,47,48],{},"int next = count + 1; \u002F\u002F Signed overflow. Optimism is not a larger type.\n",[15,50,51,52,55,56,59],{},"With ",[33,53,54],{},"\u003Climits>"," included, this compiles. The addition exceeds ",[33,57,58],{},"int","'s range: signed overflow is undefined behavior, not guaranteed wraparound. Unsigned arithmetic has different rules.",[10,61,63],{"id":62},"the-fix","The fix",[15,65,66,67,70],{},"Check the boundary ",[21,68,69],{},"before"," adding. A wider type helps only if the allowed results fit it. Use unsigned wraparound only when that's actually the intended behavior.",[15,72,73],{},"Out-of-bounds access, expired objects, and data races are other traps. Compilers optimize assuming defined code follows the rules. They haven't agreed to preserve your accident.",[15,75,76],{},"Use warnings, tests, and appropriate sanitizers. A clean run checks exercised paths, not every possible execution. Your green checkmark isn't a priest.",[10,78,80],{"id":79},"ask-your-ai","Ask your AI",[82,83,84],"blockquote",{},[15,85,86],{},"Find lifetime, bounds, overflow, and race risks. Name each violated rule, fix the cause, and propose boundary tests and sanitizer checks.",[15,88,89],{},[21,90,91],{},"“It compiles” means it compiles. Stop adding imaginary warranties.",[15,93,94,95,102,103,102,108,113],{},"Receipts: ",[96,97,101],"a",{"href":98,"rel":99},"https:\u002F\u002Feel.is\u002Fc++draft\u002Fexpr.pre",[100],"nofollow","Expression rules"," · ",[96,104,107],{"href":105,"rel":106},"https:\u002F\u002Feel.is\u002Fc++draft\u002Fintro.races",[100],"Data races",[96,109,112],{"href":110,"rel":111},"https:\u002F\u002Fclang.llvm.org\u002Fdocs\u002FUndefinedBehaviorSanitizer.html",[100],"UBSan",".",[115,116,117],"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":119},[120,121,122],{"id":12,"depth":45,"text":13},{"id":62,"depth":45,"text":63},{"id":79,"depth":45,"text":80},"It compiled. You shipped it. An inspiring chain of unsupported conclusions.","md","UNDEFINED BEHAVIOR","undefined-behavior",{},"2",true,"6","\u002Flessons\u002Fundefined-behavior",[133,140,148],{"prompt":134,"options":135,"answer":39,"explanation":139},"Signed integer addition overflows. What is guaranteed?",[136,137,138],"Wraparound, like unsigned arithmetic.","No defined result for that overflowing addition.","An exception at runtime.","Signed overflow is undefined behavior. Your CPU doing something familiar does not amend the language rules.",{"prompt":141,"options":142,"answer":146,"explanation":147},"An invalid access worked ten times. What does that prove?",[143,144,145],"Only what happened in those ten runs.","The access is valid on this compiler.","It is safe if optimization stays off.",0,"Repeated luck is still luck. You have a demo, not a correctness argument.",{"prompt":149,"options":150,"answer":45,"explanation":154},"A sanitizer found nothing. Is all undefined behavior ruled out?",[151,152,153],"Yes: every path was checked.","Yes: combining sanitizers proves correctness.","No: checks and exercised paths have limits.","Sanitizers catch many bugs on tested paths. They cannot review the executions you never ran. Neither can your victory tweet.",{"title":5,"description":123},"lessons\u002Fundefined-behavior","T267bCsRPRK6pHEP2Jpmuj6AKKCdG5-cW30JPXvhtN8",[159,163,166,170,174,178],{"lessonId":160,"slug":160,"title":161,"order":162},"ownership","Someone has to clean up.","1",{"lessonId":164,"slug":164,"title":165,"order":128},"lifetime","Your pointer outlived its welcome.",{"lessonId":167,"slug":167,"title":168,"order":169},"references-and-pointers","Borrowed doesn’t mean immortal.","3",{"lessonId":171,"slug":171,"title":172,"order":173},"raii","Make cleanup someone’s actual job.","4",{"lessonId":175,"slug":175,"title":176,"order":177},"copies-and-moves","Moving is not a magic speed spell.","5",{"lessonId":126,"slug":126,"title":5,"order":130},1789406997109]