Saturday 15 May 2010

Memory alignment causes crash using Visual C++ -


While retyping some existing code on the better stream line, I suffered an accident during my trial, which I On the issue of compressed memory alignment, but I'm not sure how to fix it without masking the problem.

I am using Microsoft Visual Studio 2008.

I have two files.

FileA.cpp

  class squares: public base class {public: a () throw (); ~ A () throw (); Virtual Init () throw (); Virtual Int start () Throw (); Virtual It Stop () Throw (); Virtual Zero Cleanup () Throw (); Public: int * pap; Handle m_evTimerStop; Unsigned char m_ctrl; CAtlList & LT; Integer & gt; M_list; }   

In FileA.cpp, when I use m_list, it's all right.

In FileB.cpp I have declared a global variable

  extern ClassA g_pCA;   

So if some functions go to use m_list:

g_pCA-> M_list.RemoveAt (pos);

It crashes if I call: g_pCA-> gt; M_list.GetCount ();

In FileA, the count will be the correct number, say 10 elements. But in FileB, the count is sometimes negative in some random numbers. You get the picture.

That's why I managed to track the fact that the file MCP link in the ACPP is at 0x9caba5 address, but the file is on BQP, M_list 0x9caba8. There is a 3 byte difference that made me doubt the memory alignment.

If I transpose the m_list above m_ctrl, then the problem is resolved,

or if I changed m_ctrl from an unsigned character to an int, then alignment The cause is clearly fixed.

I do not understand that implementing different memory alignment rules in different compiler files

Any compiler flags to make this resolution?

Thanks ... AMRO

About my estimate Correct, if #pragma pack is correct, "just do something like this, pack only those structures that really need it":

  # Pragma pack (push) # Purgama Pack (1) Straight Some _That_ needs_to_be_packed {....}; #pragma pack (pop)   

Now your data is packaged, but other data structures will not be affected.



No comments:

Post a Comment