I wish to calculate the distance between a program's bss section and the start of heap section, so I've got a program like this:
$ 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;
}
When I compile that code using:
$ g++ 1.cpp
The errors are:
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);
^
I don't think it's improper to convert an int*
to int
. I found plenty of examples doing this on the internet.
Why is my code not compiling?