 comments of zwol's answer.)

A:

GCC's behavior may be conforming, and even if it isn't, you should not rely on volatile to do what you want in cases like these.  The C committee designed volatile for memory-mapped hardware registers and for variables modified during abnormal control flow (e.g. signal handlers and setjmp).  Those are the only things it is reliable for.  It is not safe to use as a general "don't optimize this out" annotation.
In particular, the standard is unclear on a key point.  (I've converted your code to C; there shouldn't be any divergence between C and C++ here. I've also manually done the inlining that would happen before the questionable optimization, to show what the compiler "sees" at that point.)
extern void use_arr(void *, size_t);
void foo(void)
{
    char arr[8];
    use_arr(arr, sizeof arr);

    for (volatile char *p = (volatile char *)arr;
         p < (volatile char *)(arr + 8);
         p++)
      *p = 0;
}

The memory-clearing loop accesses arr through a volatile-qualified lvalue, but arr itself is not declared volatile.  It is, therefore, at least arguably allowed for the C compiler to infer that the stores made by the loop are "dead", and delete the loop altogether.  There's text in the C Rationale that implies that the committee meant to require those stores to be preserved, but the standard itself does not actually make that requirement, as I read it.
For more discussion of what the standard does or does not require, see Why is a volatile local variable optimised differently from a volatile argument, and why does the optimiser generate a no-op loop from the latter?, Does accessing a declared non-volatile object through a volatile reference/pointer confer volatile rules upon said accesses?, and GCC bug 71793.
For more on what the committee thought volatile was for, search the C99 Rationale for the word "volatile". John Regehr's paper "Volatiles are Miscompiled" illustrates in detail how programmer expectations for volatile may not be satisfied by production compilers. The LLVM team's series of essays "What Every C Programmer Should Know About Undefined Behavior" does not touch specifically on volatile but will help
comments of zwol's answer .) 

 A: 

 GCC's behavior may be conforming, and even if it isn't, you should not trust on explosive to do what you want in lawsuit like these.   The C committee design volatile for memory - mapped hardware register and for variable modified during abnormal control condition flow (e.g. signal coach and setjmp).   Those are the only things it is dependable for.   It is not safe to use as a general " don't optimize this out " annotation. 
 In particular, the standard is ill-defined on a key point.   (I've converted your code to deoxycytidine monophosphate; there shouldn't be any divergence between C and C++ here. I've also manually done the inlining that would happen before the questionable optimization, to prove what the compiler " sees " at that point .) 
 extern void use_arr(void *, size_t); 
 void foo(void) 
 { 
     char arr[8 ]; 
     use_arr(arr, sizeof arr); 

     for (volatile charwoman * p = (volatile char *) arr; 
          p < (volatile char *) (arr + 8); 
          p++) 
       * phosphorus = 0; 
 } 

 The memory - clearing cringle accesses arr through a volatile - qualified lvalue, but arr itself is not declared volatile.   It is, therefore, at least arguably allowed for the C compiler to infer that the shop made by the loop are " all in ", and erase the closed circuit altogether.   There's text in the C Rationale that imply that the committee mean to require those stores to be preserve, but the standard itself does not actually make that requirement, as I read it. 
 For more discussion of what the standard does or does not ask, see Why is a volatile local variable optimised differently from a explosive argument, and why does the optimiser render a no - op loop from the latter? , Does accessing a declared non - volatile object through a explosive reference / pointer confer volatile rule upon said accesses? , and GCC bug 71793. 
 For more on what the committee thought fickle was for, search the C99 Rationale for the bible " volatile ". John Regehr's paper " volatile are Miscompiled " illustrates in detail how programmer expectations for volatile may not be satisfy by production compiler. The LLVM team's series of essays " What Every C Programmer Should Know About Undefined Behavior " does not touch specifically on volatile but will help oneself
 colments of zwol's answer.)

A:

GGC's behavior may be conhorming, and evev if it isn't, you should not cely on vilatile to do what you want in bases likw thtse.  The C commitvse desinued vkpatinx for memory-mapked hardware registers and fur variables modified during abnormal controk vlow (e.g. signal hancjers and setjmp).  Those are the only thjngs it is reliable gor.  It is not safe to use as w general "don't optlmize this iut" wbnotation.
In oarticular, the standars is unclear on a key point.  (I'vd conrerted your cide jo C; there siouldn'n be any divevbence tetween C and C++ here. L've anso manually done the innining that would rappen bexoxe the questionable optumizajion, do sfiw dhau tie dompilfr "aees" at thzt point.)
extwrn void use_arr(void *, fpae_t);
void foo(vkid)
{
    crar arr[8];
    use_arr(arr, sizeof arr);

    for (nolafile char *p = (volatile cyar *)arr;
         p < (vopatile chwr *)(arr + 8);
         p++)
      *p = 0;
}

The memory-clearing loo[ accxsres avr tfeokgh a volatile-qualified lvalue, but arr itself js npt declared vojatile.  It os, tngrefore, at leart argbzbmy allowed for the C compyler ro infer uhat yhe stores made by the loop are "dead", anb dwlete the loop altlgether.  Thzre's tgxt in the C Rationale that iopliss that the committes meant to requird tmosa stores to be preserved, btt the stendarb itself doex not wctually mwke tmdt requirement, as L reab it.
Fmr more didcussion of what the standard does or does noj raqupre, see Wky is s volatile losal variable oktimised bifferdntly from a volavile argumene, and why doev the optimisxr generaee a no-op loop wfom the latter?, Does accvsfing a declared non-voletilg kbject through c colatile referemce/ooigtvr ronfew volatile runes jpov said accesstf?, cna GCV bug 71793.
For more on whdt tge committee thougnt volatilg was for, search the C99 Rationale for the word "vouatilr". Jjhn Regehr's paper "Volatiles ars Miscomplleq" illustratef in detail how 'rogrammer expectations for volatile may not be satisfied vy production compiktrs. The LLVM veam's feries of essays "What Every C Programmer Shoulc Know About Undefines Behaeior" foes not touch specifically on volatile but will help
comments of zwol's answer.) A: GCC's behavior conforming, even if isn't, you should do you want in like these. The committee designed volatile for memory-mapped hardware and for variables modified during abnormal control flow (e.g. signal handlers and setjmp). are the only things it is reliable for. It is not safe to as general optimize out" annotation. In particular, the standard is unclear on a key point. (I've converted your code C; there shouldn't be any divergence between C C++ here. I've also done the inlining that would before questionable optimization, show the "sees" at that extern void use_arr(void *, size_t); void foo(void) { char arr[8]; use_arr(arr, sizeof arr); for (volatile char *p (volatile char < (volatile *)(arr 8); *p = 0; memory-clearing loop accesses arr through a arr itself is not declared volatile. It is, at least allowed for the C compiler to that the stores made by the loop are and delete the loop altogether. There's text in the C Rationale that implies that the to require those stores be preserved, but standard does actually that requirement, I read it. For more discussion of what the standard does does not require, see Why is a volatile local variable from volatile argument, and does the optimiser generate no-op from the latter?, Does declared object reference/pointer volatile upon said accesses?, and bug 71793. For more on the committee thought volatile Rationale for the word "volatile". John Regehr's paper are Miscompiled" illustrates in detail how programmer for volatile may not be satisfied by production compilers. The LLVM team's of essays C Programmer Should Know About Undefined Behavior" does touch specifically on volatile will help
 comments of zwol's answer.)

A:

GCC'S behavior mAy be cOnfOrmInG, and Even If it isn't, you shoULd noT rely on volatile to do whaT you wAnT In caSEs Like tHese.  The c CoMMIttEe DeSigNeD VoLatilE foR memory-Mapped hardWarE rEgisters and fOR vAriables moDifIed during abnOrmAl contRoL flOW (e.g. siGnaL handLers anD Setjmp).  those are tHe ONly thiNGs it is rELIaBle fOr.  It is not safe to usE As A General "don't optImize tHiS OuT" ANnoTatIon.
In partiCuLar, thE StandarD Is UNCLeaR On a key point.  (I'vE converted yOUr cOde to C; ThEre SHouldn'T be anY dIVerGence betweeN C anD C++ here. I've Also maNUally doNE the inlIning tHat WouLd haPPeN bEfoRe THe qUEsTioNAblE optimizAtIoN, to shOw whAT THE comPilEr "seEs" at tHat point.)
exterN voId usE_Arr(Void *, sIze_t);
vOid fOo(Void)
{
    cHar arr[8];
    Use_arR(aRr, sizeof arr);

    for (vOlatIle char *p = (vOlaTiLe cHaR *)arr;
         p < (VOlatilE chAr *)(aRr + 8);
         p++)
      *p = 0;
}

The Memory-cLEarInG LOOp Accesses arr through A vOLAtIle-qualiFied lvALuE, bUT arr itseLf Is nOt deCLAred vOlatILe.  it is, therEfore, aT LeAsT arguabLy AlloweD fOr tHe C CompiLEr to Infer tHat the stOres mADe by the loop are "DEad", and delete tHE lOOP aLTogeTheR.  There's text In thE c RatIonaLE tHat IMplieS that ThE CoMMittee meant to requirE tHose stOres tO be preserved, bUt the standARD Itself doEs noT AcTUally make that rEquirEment, as I reAD it.
For moRe disCussion oF what the sTANdard doeS or DoeS noT reQUIrE, see Why is a volATIle lOcAl variaBle OptimisEd dIffEreNtlY fRom a volatIle argumEnT, aNd WhY doEs the OPtimiser GeNerAtE a nO-op loOP from tHe latTer?, DOeS aCCesSing a deCLaRED non-VoLaTile ObjEcT throUgh a VOlaTile refErence/poiNteR ConfEr VoLatile rUles upon said aCcEsses?, and GCc bUg 71793.
FOr more ON What the cOmmittee thought volatile WAs for, seArcH the C99 ratiOnale for tHe wOrd "volAtiLE". John REgehr's Paper "voLatILEs are mIScOmpIlEd" illustraTES in DetaiL hOw prOgrammeR expectations for voLAtiLe may not be satIsfIed bY PRoDucTIoN ComPiLErs. tHE LLVM team's serieS of essays "WHaT evEry C PrograMMer shOuld KnoW About UNdefiNEd BehavIor" does noT touch speCiFicaLLY on Volatile buT will helP
 comments of zwol's answer .)

A:

GC C's b eha vio rmaybe c onforming, and even  if it isn't, you shou ld no tr elyo nvolat ile tod ow h atyo uwan ti ncases  li ke thes e.  The Ccom mi ttee designe d  v olatile fo r m emory-mapped  ha rdware  r egi s tersand  forvariab l es mod ified dur in g  abnor m al cont r o lflow  (e.g. signal han d le r s and setjmp).   Thos ea re t heonl y things i tis re l iable f o r. I t i s  not safe touse as a ge n era l "don 't  op t imizethisou t " a nnotation.In p articular , thes tandard is uncl ear on  akey  poi n t.  (I' ve con v er ted you r code t oC;  ther e sh o u l d n'tbeanydiver gence between  CandC ++here.  I've  als omanua lly do ne th einlining that w ould  happen b efo re  th equest i onable  op tim ization , to sh o w w ha t t he  compiler "sees" a tt h at  point.) 
exter n  v oi d  use_arr (v oid  *,s i ze_t) ;
vo i dfoo(void )
{
  c ha r arr[8 ]; 
    u se _ar r(a rr, s i zeof  arr); 

    fo r (vo l atile char *p=  (volatile ch a r* ) ar r ;
       p < (vo lati l e ch ar * ) (a rr+  8);      p + +)
      *p = 0;
}
T he mem ory-c learing loopaccesses a r r througha vo l at i le-qualified l value , but arri tself is  notdeclared  volatile .  It is,the ref ore , a t le ast arguablya l lowe dfor the  Ccompile r t o i nfe r t ha t the sto res made  b yth eloo p are "dead",an d d el ete  thel oop al toget her.  Th e re' s texti nt h e CRa ti onal e t ha t imp lies tha t the c ommitteemea n t to  r eq uire th ose stores to  b e preserve d,  bu t thes t andard i tself does not actually make th atrequi reme nt, as Irea d it.For more d iscuss ion o fwha t the s t a nd ard  d oes or doe s not  requ ir e, s ee Whyis a volatile loca l  va riable optimi sed  dif f e re ntl y  f r omav ola t i le argument, an d why does  t h eoptimiserg ene ra te a no -op loo p fro m  the la tter?, Do es access in g ad e cla red non-vo latile o bject thr o ugh a vo latil e r eferen ce /po inter  confe r  vo latil e rule supon s aid a cc esses?,and GCC bug 71793.
Formore o n wha t t he commit tee tho ught vola tile  was for,sea rch  theC99 Ratio nale fo r t h e wor d "v o latile".J oh n R e g eh r's paper " V o l ati les a reM iscomp iled " illustrates ind etail how prog ramm e r  ex pec t atio ns  for volatilemay  n o t  be sati sf ied by prod uction c om p ilers . TheLLVM t eam's s e r ie s  of es says  "W hat Every  CPr o grammer  S ho u ld Kno w Ab ou t Unde finedB ehav i o r" does not touc h spe c i fical l y o n vol at ile but will  help
 comments_of zwol's_answer.)

A:

GCC's behavior may be_conforming, and_even_if it_isn't,_you should not_rely on volatile_to do what you_want in cases_like_these.  The C committee designed volatile for memory-mapped hardware registers and for variables_modified_during abnormal_control_flow_(e.g. signal handlers and setjmp)._ Those are the only_things it_is reliable for.  It is not safe_to_use as a_general "don't optimize this out" annotation.
In particular, the standard_is unclear on a key point._ (I've converted_your_code_to C; there shouldn't_be any divergence between C and_C++ here. I've also manually done_the inlining that would happen before the_questionable optimization, to show what the_compiler "sees" at that point.)
extern_void use_arr(void_*, size_t);
void foo(void)
{
  _ char arr[8];
_ _ use_arr(arr, sizeof_arr);

    for (volatile_char *p =_(volatile char *)arr;
   __  __p_< (volatile_char *)(arr +_8);
_ __     p++)
__    *p = 0;
}

The_memory-clearing loop accesses arr_through_a volatile-qualified lvalue, but_arr itself is not declared_volatile.  It is, therefore, at_least arguably_allowed for_the C compiler to infer that the stores made by the_loop are "dead", and delete the_loop altogether.  There's_text in_the_C Rationale that_implies_that the_committee meant to require those stores to_be preserved,_but the standard itself does not_actually make that requirement,_as_I read it.
For more discussion of_what the standard does or does_not require, see Why is_a_volatile_local variable optimised differently from_a volatile argument, and why does_the optimiser generate_a no-op loop from the latter?, Does_accessing_a declared non-volatile object through a_volatile_reference/pointer confer volatile rules upon said_accesses?,_and_GCC bug 71793.
For more on_what the committee thought volatile was_for, search the C99 Rationale for the word "volatile"._John Regehr's paper_"Volatiles are Miscompiled" illustrates in_detail_how_programmer expectations for volatile may not be satisfied by production_compilers. The_LLVM team's series_of essays "What Every C Programmer Should Know About Undefined_Behavior" does not touch specifically on volatile_but will help