Note that there are some explanatory texts on larger screens.

plurals
  1. POMis-indexed char[] arrays in Visual Studio 2005
    primarykey
    data
    text
    <p>I'm at a loss to explain what seems to be mis-addressing within the <code>char[]</code> arrays of a C++ class I've inherited. I'm using Visual Studio 2005 and have narrowed the problem down to this:</p> <p><strong>MyClass.h:</strong></p> <pre><code>class MyClass { public: MyClass(); virtual ~MyClass(); void Reset(); // More member functions. . . char m_szString[128]; // More member variables. . . } </code></pre> <p><strong>MyClass.cpp:</strong></p> <pre><code>MyClass::MyClass() { Reset(); } MyClass::Reset() { m_szString[0] = 'X'; } // . . . </code></pre> <p>As I single-step through the program, I find that the <code>Reset()</code> function actually sets <code>m_szString[</code><strong><code>4</code></strong><code>]</code> to <code>'X'</code> &mdash; not <code>m_szString[</code><strong><code>0</code></strong><code>]</code> as I expected. According to the watch window, the only element in the class before <code>m_szString[]</code> is a pointer to the vftable, <code>__vfptr</code>, which happens to be 4 bytes. </p> <p>If I add more member variables to <code>MyClass</code>, subsequent strings are mis-addressed by various, always-increasing multiples of 4 bytes. Not simply <em>aligned</em> to 4-byte boundaries, but actually <em>offset</em> by multiples of 4. It's as if the compiler is skipping twice the necessary space for each vftable ... but that's purely a guess.</p> <p>Somewhat similar problems have been reported (Google, MSDN), but I haven't found any answers.</p> <hr> <p><strong>Additional Information / Partial Solution:</strong> The class is the only member variable of a wrapper class that becomes a DLL. The parent was originally declared as</p> <pre><code>class ATL_NO_VTABLE CWrapperClass </code></pre> <p>Removing <code>ATL_NO_VTABLE</code> fixed the alignment problem.</p> <p>I'm still seeing buffer overflows, but those should be fairly easy to track down.</p> <p>Can you explain <code>ATL_NO_VTABLE</code> in terms understandable to an embedded C developer who's had very limited experience with COM beyond BSTRs, or better yet, provide a pointer (sorry) to a good reference?</p> <hr> <p><em>Still More:</em> <a href="https://stackoverflow.com/questions/7525/">This question</a> provides some helpful debugging information.</p> <p>Thanks for your help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload