I'm having simple code as follows:
#include<stdio.h>
int glob;
int main(void)
{
int a;
printf("&a is : %p \n", &a);
printf("glob is : %p \n", &glob);
return 0;
}
Output of above program is: First run:
&a is : 0x7fff70de91ec
glob is : 0x6008f4
Second run :
&a is : 0x7fff38c4c7ac
glob is : 0x6008f4
I'm studying about virtual & physical addresses. I have following question:
In executed this program on Linux : 2.6.18-308.el5 x86_64 GNU/Linux
Compiled using : gcc version 4.1.2 20080704 (Red Hat 4.1.2-52)
In C we have seen different format specifiers. Here we will see another format specifier called %p. This is used to print the pointer type data.
By using '%p' you print the address of the variable in question, "The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx)."
Difference between %p and %x in C/C++The %p is used to print the pointer value, and %x is used to print hexadecimal values. Though pointers can also be displayed using %u, or %x. If we want to print some value using %p and %x then we will not feel any major differences.
Both addresses are virtual.
Modern systems uses stack randomization to prevent so-called stack-smashing attacks, which is why the local variable can change its location every run. However the global variable is stored in the executable and is loaded at the same offset every time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With