- 论坛徽章:
- 2
|
我想计算一下一个程序的bss段和堆的底部之间的距离,看看sbrk最多能把数据段扩大多少。代码如下:
- $ cat 1.cpp
- #include<unistd.h>
- #include<stdlib.h>
- #include<stdio.h>
- int empty;//bss
- int main()
- {
- char*p=(char*)malloc(0);
- printf("%d\n",(int)p-(int)&empty);
- return 0;
- }
复制代码
编译:
- $ g++ 1.cpp
- 1.cpp: In function ‘int main()’:
- 1.cpp:8:22: error: cast from ‘char*’ to ‘int’ loses precision [-fpermissive]
- printf("%d\n",(int)p-(int)&empty);
- ^
- 1.cpp:8:30: error: cast from ‘int*’ to ‘int’ loses precision [-fpermissive]
- printf("%d\n",(int)p-(int)&empty);
- ^
复制代码
为什么不能强转指针类型到int,我的程序应该如何修改才能达到预期?
谢谢。
|
|