【发布时间】:2014-10-12 18:09:05
【问题描述】:
char *s = sbrk(some_num);
if (s != (char *)-1) {
some_struct = (SomeStruct *)s;
some_struct->num = 8; //num is an int [*SEG FAULT OCCURS ON THIS LINE*]
}
使用 gdb 调试:
p some_struct => (SomeStruct *) 0xef7100
p some_struct->num => 0
从之前的 q/a 中,我了解到当某些内容为 NULL 时会发生 seg 错误,但在此示例中,some_struct、some_struct->num 和 8 都是 non-NULL。。 p>
为什么会出现分段错误?如何将 some_struct->num 设置为某个 int 值?
【问题讨论】:
-
Seg 错误不仅在指针为 NULL 的情况下,还在于您对内存的访问错误。
-
请发布可以在实践中验证的最少量代码。
-
要么 0xef7100 不是有效指针,要么 num 是 *int 而不是 int
-
更新问题,提供更多细节
-
错误的内存访问/不是一个有效的指针 --- 谢谢,我会试着调查一下
标签: c