What is volatile in C ++ used? (Not reading)

Source: Internet
Author: User
  • Question:What is volatile in C ++ used? (Not reading)
  • Original Discussion link:Http://community.csdn.net/expert/topicview1.asp? Id = 4340502
  • Forum:Newbie ParkAudit Team:C/C ++
  • Questioner:IwodgirlReleased:Goodluckyxl
  • Thanks:Codearts, snowbirdfly, goodluckyxl
  • Keywords:
  • Answer:

    # Include <iostream. h>
    # Include <stdio. h>
    Class volatilefun {
    Public:
    Int val;
    Volatile int F8 ();
    };

    Volatile int volatilefun: F8 () {return val ;}

    void main ()
    {< br> volatilefun s;
    S. val = 89;
    volatile int I = S. f8 ();
    I = 23; // added by myself, can also run, and changed the value of I
    cout getchar ();
    }

    If you have any questions, contact us! Thank you.
    ---------------------------------------------------------------
    1. Volatile ensures that the accessed variables are not optimized by the compiler.
    For example, the requested variable A = 1. If the embedded assembly is used, the compiler is released.
    Change content 1 of address
    And if you do not add volatile, it's still the original 1.
    If this parameter is added, the data is read from the address of.
    4. Register operations must be added! General driverProgramThe port operation in also needs to be added. Volatile only ensures that every time you take the variable address, it is the value of the source address of this variable! For example, declare a variable uint16 portaddress = 0x0001; the followingCodePortaddress is no longer assigned a value. If volatile is not added, the system will take 1 directly every time you use the portaddress variable, instead of getting the value from the portaddress address, if portaddress is related to hardware, it may change its value due to hardware reasons (no longer 0x0001), resulting in an error in running!

    My View:
    Simply remove the compiler from any Optimization of this variable.
    All operations on this data are read and written to the corresponding address and are not taken from the registers temporarily saved for optimization.

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.