{"record":{"id":"db8bcbb380f7904c","repo":"go-delve/delve","slug":"can-not-take-address-of-q","errorCode":null,"errorMessage":"can not take address of %q","messagePattern":"can not take address of %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2343,"sourceCode":"\t\tval, ok := constant.Uint64Val(xev.Value)\n\t\tif ok && val == 0 {\n\t\t\tstack.err = fmt.Errorf(\"couldn't read pointer: %w\", xev.Unreadable)\n\t\t\treturn\n\t\t}\n\t}\n\trv := &xev.Children[0]\n\tif rv.Addr == 0 {\n\t\tstack.err = errors.New(\"nil pointer dereference\")\n\t\treturn\n\t}\n\tstack.push(rv)\n}\n\n// Evaluates expressions &<subexpr>\nfunc (scope *EvalScope) evalAddrOf(op *evalop.AddrOf, stack *evalStack) {\n\txev := stack.pop()\n\tif xev.Addr == 0 || xev.DwarfType == nil {\n\t\tstack.err = fmt.Errorf(\"can not take address of %q\", astutil.ExprToString(op.Node.X))\n\t\treturn\n\t}\n\n\tstack.push(xev.pointerToVariable())\n}\n\nfunc (v *Variable) pointerToVariable() *Variable {\n\tv.OnlyAddr = true\n\n\trv := v.newVariable(\"\", 0, godwarf.FakePointerType(v.DwarfType, int64(v.bi.Arch.PtrSize())), v.mem)\n\trv.Children = []Variable{*v}\n\trv.loaded = true\n\n\treturn rv\n}\n\nfunc constantUnaryOp(op token.Token, y constant.Value) (r constant.Value, err error) {\n\tdefer func() {","sourceCodeStart":2325,"sourceCodeEnd":2361,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2325-L2361","documentation":"Thrown by evalAddrOf when evaluating &x: the operand has no address (xev.Addr == 0) or no DWARF type (xev.DwarfType == nil), so Delve cannot form a pointer to it. Values that live only in CPU registers, literals, and constants are not addressable in the debuggee's memory.","triggerScenarios":"Evaluating `&x` where x is a constant, a literal (e.g. `&5`), a variable held entirely in a register (optimized code / register-allocated local), or a value reconstructed by Delve without a backing address. Also for temporaries like `&f()` where the function returns a value Delve did not materialize in memory.","commonSituations":"Trying to take the address of a small local int the compiler kept in a register in an optimized build, taking the address of a function call result at the dlv prompt, or `&someConstant`.","solutions":["Avoid optimized builds while debugging: compile with `-gcflags='all=-N -l'` (dlv debug does this by default; use `dlv exec` on a binary built this way) so variables stay in memory and are addressable.","Assign the value to a variable in the program first, then take that variable's address.","If you just need the value, print it directly instead of forming a pointer.","Step to a point in the function where the variable is live in memory (after it escapes or is used addressably)."],"exampleFix":"// before (i kept in register, optimized build)\n(dlv) print &i\ncan not take address of \"&i\"\n\n// after: rebuild unoptimized\ngo build -gcflags='all=-N -l' -o app .\ndlv exec ./app\n(dlv) print &i","handlingStrategy":"validation","validationCode":"// Only take addresses of variables with backing memory.\n// In the program, ensure the value is stored in a variable:\nx := 42\npx := &x // valid in code; at the prompt use `print &x` only for addressable vars","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Debug with -gcflags='all=-N -l' so locals stay in memory and are addressable.","Never take the address of literals or function-call results at the prompt.","Assign the value to a variable in the program first."],"tags":["debugger","expression-evaluation","address-of","compiler-optimization"],"backgroundTag":"value-not-addressable","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}