# 恐慌：运行时错误：cgo参数包含指向Go指针的Go指针

- **ID:** `go/cgo-char-array-null-termination`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

将Go切片或字符串作为`char*`传递给C时未添加空终止符，导致C读取越界。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **Use `C.CString` which allocates a null-terminated C string** (95% 成功率)
   ```
   cs := C.CString("hello")
defer C.free(unsafe.Pointer(cs))
C.printString(cs)
   ```
2. **Manually append null byte and pin** (85% 成功率)
   ```
   b := []byte("hello\x00")
C.printString((*C.char)(unsafe.Pointer(&b[0])))
   ```

## 无效尝试

- **Using `C.CString` but forgetting to free** — Memory leak, but the null termination is correct; this error is about pointer nesting. (40% 失败率)
- **Passing `&s[0]` directly** — Go string data is not null-terminated. (80% 失败率)
