c++ - inline functions returning incorrect results -


i have large application working on in c++, , have class inline functions returning wrong value. looks offset 1 entry.

here example of how code set up:

class test {  private:     uint myval1;         uint myval2;     uint myval3;     uint myval4;  public:     uint myfunct1() const { return myval1 };     uint myfunct2() const { return myval2 }; }; 

what seeing myfunct1 returns myval2 , myfunct2 returns myval3. if dont make functions inlined works expected.

any ideas on why happening?

thanks in advance.

(i assume posted above fragment header file.)

things typically happen when different source files in program compiled different memory-layout-related settings, class packing , alignment settings. header file included these different translation units , interpreted differently because of discrepancies in memory-layout settings.

once start passing test objects between these translation units, problem reveals itself. 1 translation unit creates test object 1 memory layout, translation units reads or writes assuming totally different memory layout. in case inline functions interpreted differently in each translation unit.

if define member functions non-inline ones, assume class memory layout specific source file in defined. sweep problem under carpet , make things "work" (since access functions tied 1 memory layout), nevertheless still not situation have. still can lead various problems of similar nature down road.

make sure source files in program compiled same class memory layout settings.

p.s. fred noted in comments, discrepancy in class memory layout between translation units can caused prosaic forgetting recompile source file after modifying header file source file depends upon.

another "popular" source of such problems class definitions depend on preprocessor directives (i.e. class layout "customized" #ifdef/#endif segments). if forget #define important in source file includes header file, might end different memory layout class in source file.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -