聪明文档网

聪明文档网

最新最全的文档下载
当前位置: 首页> volatile 用法

volatile 用法

时间:2023-02-04 06:33:42    下载该word文档
嵌入式编程中经常用到volatile这个关键字,在网上查了下他的用法找到了下面这3篇文章比较好的:一:告诉compiler不能做任何优化比如要往某一地址送两指令:int*ip=...;//设备地址*ip=1;//第一个指令*ip=2;//第二个指令以上程序compiler可能做优化而成:int*ip=...;*ip=2;结果第一个指令丢失。如果用volatile,compiler就不允许做任何的优化,从而保证程序的原意:volatileint*ip=...;*ip=1;*ip=2;即使你要compiler做优化,它也不会把两次付值语句间化为一。它只能做其它的优化。这对devicedriver程序员很有用。二:表示用volatile定义的变量会在程序外被改变,每次都必须从内存中读取,而不能把他放cache或寄存器中重复使用。volatilechara;a=0;while(!a{//dosomethings;}doother(;如果没有volatiledoother(不会被执行还找到一篇比较详细的比较CONSTVolatile的文章8.4.ConstandvolatileThesearenewinStandardC,althoughtheideaofconsthasbeenborrowedfromC++.Letusgetonethingstraight:theconceptsofconstandvolatilearecompletelyindependent.Acommonmisconceptionistoimaginethatsomehowconstistheoppositeofvolatileandviceversa.Theyareunrelatedandyoushouldrememberthefact.Sinceconstdeclarationsarethesimpler,we'lllookatthemfirst,butonlyafterwehaveseenwherebothofthesetypequalifiersmaybeused.Thecompletelistofrelevantkeywordsis
charlongfloatvolatileshortsigneddoublevoidintunsignedconstInthatlist,constandvolatilearetypequalifiers,therestaretypespecifiers.Variouscombinationsoftypespecifiersarepermitted:char,signedchar,unsignedcharint,signedint,unsignedintshortint,signedshortint,unsignedshortintlongint,signedlongint,unsignedlongintfloatdoublelongdoubleAfewpointsshouldbenoted.Alldeclarationstodowithanintwillbesignedanyway,sosignedisredundantinthatcontext.Ifanyothertypespecifierorqualifierispresent,thentheintpartmaybedropped,asthatisthedefault.Thekeywordsconstandvolatilecanbeappliedtoanydeclaration,includingthoseofstructures,unions,enumeratedtypesortypedefnames.Applyingthemtoadeclarationiscalledqualifyingthedeclarationthat'swhyconstandvolatilearecalledtypequalifiers,ratherthantypespecifiers.Hereareafewrepresentativeexamples:volatilei;volatileintj;constlongq;constvolatileunsignedlongintrt_clk;struct{constlongintli;signedcharsc;}volatilevs;Don'tbeputoff;someofthemaredeliberatelycomplicated:whattheymeanwillbeexplainedlater.Rememberthattheycouldalsobefurthercomplicatedbyintroducingstorageclassspecificationsaswell!Infact,thetrulyspectacularexternconstvolatileunsignedlongintrt_clk;isastrongpossibilityinsomereal-timeoperatingsystemkernels.8.4.1.ConstLet'slookatwhatismeantwhenconstisused.It'sreallyquitesimple:constmeansthatsomethingisnotmodifiable,soadataobjectthatisdeclaredwithconstasapartofitstypespecificationmustnotbeassignedtoinanywayduringtherunofaprogram.Itisverylikelythatthedefinitionoftheobjectwill

免费下载 Word文档免费下载: volatile 用法

  • 29.8

    ¥45 每天只需1.0元
    1个月 推荐
  • 9.9

    ¥15
    1天
  • 59.8

    ¥90
    3个月

选择支付方式

  • 微信付款
郑重提醒:支付后,系统自动为您完成注册

请使用微信扫码支付(元)

订单号:
支付后,系统自动为您完成注册
遇到问题请联系 在线客服

常用手机号:
用于找回密码
图片验证码:
看不清?点击更换
短信验证码:
新密码:
 
绑定后可用手机号登录
请不要关闭本页面,支付完成后请点击【支付完成】按钮
遇到问题请联系 在线客服