gdb 设置了 disable-randomization off, 那么数组依然打印的这些固定地址是为什么呢?{0xad8010, 0x500000000, 0x0, 0x7f889b8fc76d, 0x0}

2019-04-10 20:16:58 +08:00
 Kristd
int main() {
    int* p = 0;
    p = (int*)malloc(5*sizeof(int));
    unsigned int i = 0;

    for (i = 0; i < 5; i++) {
        p[i] = i+1;
    }

    free(p);
    return 0;
}

每次执行的打印结果

(gdb) p p@5
$3 = {0xad8010, 0x500000000, 0x0, 0x7f889b8fc76d, 0x0}

(gdb) p p@5
$4 = {0x1522010, 0x500000000, 0x0, 0x7f58f8f1676d, 0x0}

首地址变成动态了, 后面的固定地址是什么原理

1467 次点击
所在节点    程序员
3 条回复
bumz
2019-04-10 20:23:00 +08:00
The addresses of stack variables will not change across different debugging sessions.

http://visualgdb.com/gdbreference/commands/set_disable-randomization

你这是堆的,取决于 glibc 的 malloc 怎么实现,和 kernel 提供的 aslr 没关系
ashlord
2019-04-10 20:48:29 +08:00
p@5 没有意义吧,显示的实际是*(&p + 0)到*(&p + 4)的值,而不是 p 指向的 array 后续元素的地址啊
amosasas
2019-04-11 01:10:40 +08:00
@bumz 学到了。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/553846

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX