Reproduced const differences in C and C + +

Source: Internet
Author: User

Reprinted from Csdn

http://blog.csdn.net/huangshanchun/article/details/41215065

const differences in C and C + +

Test environment: Windows 7 vs2010

Using the const modifier variable means that it is read-only, but the const in C is quite different from the const in C + +.

C Language Const is "counterfeit", see the following C program:

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main ()
  4. {
  5. const INT a = 5;
  6. int *p = NULL;
  7. printf ("A before modification:%d \ n", a);
  8. p = (int *) &a;
  9. *p = 6;
  10. printf ("a modified:%d \ n", a);
  11. System ("pause");
  12. return 0;
  13. }

Test results:

The value of a in the program can be modified, although we cannot directly modify, but can be obtained by obtaining the address of a to indirectly modify.

The same program we implemented in C + +:

[CPP]View Plaincopy
  1. #include <iostream>
  2. Using namespace std;
  3. int main ()
  4. {
  5. const INT a = 5;
  6. int *p = NULL;
  7. cout<<"a modified before:" <<a<<endl;
  8. p = (int *) &a;
  9. *p = 6;
  10. cout<<"a modified:" <<a<<endl;
  11. System ("pause");
  12. return 0;
  13. }

Test results:

C + + and C do not work the same, and the C + + compiler handles const constants by placing constants in the symbol table when a constant declaration is encountered, and when a constant is found in the compilation, it is replaced directly by the values in the symbol table.

Question: How to interpret the address, the compilation process if you find that the const using extern or the & operator, the corresponding constant allocation of storage space (in order to be compatible with C). Although the C + + compiler may allocate space for Const constants (for & Operations), it does not use the values in its storage space.

Conclusion:

const constant in C: It is a read-only variable, has its own storage space, can be indirectly modified by the address of its value.

Const constants in C + +: They are placed in the symbol table, may allocate storage space, may not allocate storage space, and a temporary memory space is allocated when the & symbol is used.

Reproduced const differences in C and C + +

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.