I have executed the below programm on both C and C++ compiler and got different results
#include <stdio.h>
int main()
{
const int a=10;
int *p=(int*)(&a);
*p = 20;
printf("address of a=%u\t%d\n", &a,a);
printf("address of p=%u\t%d\n", p,*p);
return 0;
}
With C compiler I got the output
address of a=1245052 20
address of p=1245052 20
With C++ compiler I got
address of a=1245052 10
address of p=1245052 20
Can anybody tell me why the different value for *p and a even thiough both are pointing to the same memory location?
I think you are encountering what is commonly called "undefined behavior" or "implemention specific". In C, const int a points to a memory location that can be modified, but in c++ variable a is not located at any memory variable but just used as an immediate value, which is similar to just hard-coding a literal value in the program. You could have rewritten the lines like this:
c++
printf("address of a=%u\t%d\n", &a,10);
c
printf("address of a=%u\t%d\n", &a,a);
Can you tell me where the variable be put when it is declared as const. Will it go into const segment.
"You can also specify sections for initialized data ( data_seg ), functions ( code_seg ), and const variables (const_seg)."
The above is an extract from msdn. If it goes into a particular segment of memory, why the value is not getting changed when modified thru a pointer.
From your expalnation what I understood is that the const variable is replaced by the value whenever it is encountered by the compiler.
i.e it is not stored at a specific location.
Now if I use C compiler why is the value getting changed or why it is behaving differently?
Actually the memory location for a does get modified in Visual C++. But this is implementation specific. It may not be so in gcc. But the catch is that Visual C++ never uses the value stored in a 's memory location. It is allowed to do so by the C++ standard. That is because if you declare a as const int , that means you are never going to change it's value. So as an optimization, the compiler can use the literal directly instead of the actual value stored in a . You can see this fact if you take a look at the assembly code for your program.
I have attached the assembly files in text format and line 7 is the relevant portion.
Your explanation was very usefull. I have one more doubt in the same context.
This assembly thing is quite new for me. In the cpp assembly line 7 states push 10.
So does that mean in Cpp the constant variable are also pushed onto the stack?????
I can't say anything about cpp. That is not specified in the standard. It only happens in Visual C++. It may be different in gcc. But in VC++, the variable is not pushed into the stack. Only the literal it holds. In this case it is 10.