What are the values of one address? How is it possible !!!

Source: Internet
Author: User

The reason is as follows:

One student wroteProgram,CodeAs follows:

# Include <stdio. h>
Int Main ( Void )
{
Const Int A = 10 ;
*( Int *) & A = 20 ;
If (A = 20 )
Printf ( " A = 20 \ n " );

printf ( " % d, % x \ n ", A, & );;
printf ( " % d, % x \ n ", * ( int *) & A, ( int *) & );
return 1 ;
}

I believe everyone knows the output result of the program. I also posted 10, 31f73c 20, 31f73c
the student was a little embarrassed when he saw the result. He asked:
" How can there be two values for the same address?"
at that time, when I first thought that he was compiled using the C language compiler, I asked him how he could pass the compilation phase? (Note: The school teaches mtk development and the development language uses the C language, so it is the first priority .)
later I checked the file type. It turns out to be a. cpp file. Visual c ++ 6.0 by default, the format of the new source file is CPP, and the compiling is of course the C ++ compiler. If the file name is clear. C (such as test. c), then the C language compiler will be used for compilation.

The key in this applet is const. If you remove it, then every C can understand it. There are many in-depth explorations on the Internet about the usage of Const. I will not discuss it in detail here. Please refer:
1. http://www.vckbase.com/document/viewdoc? Id = 412 --------Const Usage Details
2. http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html ------ const keywords in C/C ++
One point: in C, const is modified to"Read-OnlyVariable"; In C ++, modify it to"Constant".

Constants cannot be changed in general, that is, they cannot be assigned values. But this is not done in the program. Instead, the address of a is taken out directly, and 20 is directly put in the memory according to the address. This is no problem. When I said this, this student asked: "Have you said that the value of a has changed? So why is if determined to be false ?". I then told him that A is not a value, but the code of an address in the memory. As

If (A = 20 ) It is true that 10 is not equal to 20. When the VC compiler compiles the code, it will have a" Source codeOptimization When it detects constant

All are replaced with 10, so if statements are judged to be false, which can be seen from the second print statement. The last print statement indicates that the value in the memory address represented by a must be 20 instead of 10.
The debugging result of this applet cannot be seen, and the result is the same as that of the print, but the disassembly can be used to look at the clues. If you are interested, you can look at the assembly code by yourself.
To sum up,Of course, the same address cannot store two different values.! Never be cheated by the compiler !!!

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.