I'm getting familiar with C and I was going over the memory layout of a program. I did manage to get some info about where variables go inside the memory, but I still got a few that remain unclear to me.
I ran this on lubuntu 18.04 running on virtualbox and windows 10 as a host
Say I have the following program:
int foo(); // (??)
void point(void* p); // (??)
int data1; // bss segment
int data2 = 3; // data segment
int main(){
char str[3] = {'a','b','c'}; // text segment(??)
char *str1 = "word"; // str1 - stack, *str1 = 'w' - text segment (??)
point(&data1); // call stack
return 0;
}
void point(void* p){
long dist1 = (size_t)&data2 - (size_t)p; // call stack - inside point's AF: dist1, p
printf("%ld\n", dist1); /* is a long integer generally enough to hold addresses (or difference of
addresses). is it legal to calculate differences of addresses of different
segments? */
}
int foo(){
return 0;
}
I'd like some help with the questions I've added in comments, and maybe some confirmation for the what I already did.
Thanks in advance.