c++ - What is the rationale for this undefined behavior? -


warning[...]: undefined behavior: order of volatile accesses undefined in statement x.cpp xxx

why line undefined behavior?

  case 2:     vdda = 3.3 * (*vrefint_cal) / adc_dr->data; 

where declarations/initializations are:

volatile short const *vrefint_cal = (short *) 0x1ffff7ba; 

and

volatile struct_adc_dr *adc_dr = (struct_adc_dr*) 0x40012440; 

defined by:

typedef struct {   unsigned data         : 16;   unsigned              : 16; } struct_adc_dr; 

is because compiler isn't sure volatile elements act diferent in order accessed? (what case)

but shouldn't ensured calculation gets performed left right operators have same priority?

volatile implies compiler reading not ordinary memory address, i/o port. 2 such reads, want reads happen in order.

in both c , c++, evaluation order of operands not defined. if helps you, think of division function call:

vdda = 3.3 * divide(*vrefint_cal, adc_dr->data); 

the point now, volatile, it's order important, might not want leave decision compiler. warns it.

to rid of warning, make order explicit introducing additional sequence points code. instance:

short const x = *vrefint_cal; unsigned const y = adc_dr->data; vdda = 3.3 * x / y; 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -