Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory Alignment issue with std::map on Linux
    primarykey
    data
    text
    <p>I have run into a problem while working with c++ over Linux.</p> <p>I have a base Message class which looks like this:</p> <pre><code>class MsgBase { public: MsgBase( unsigned int msgid ); map &lt; unsigned int, MSGIEBase* &gt; messageIE_Map; // map for IEs unsigned int messageId; // 32 bit message id }; </code></pre> <p>class Der1 is derived from MsgBase and looks like:</p> <pre><code>class Der1 : public MsgBase { public: Der1 (); virtual ~Der1 (); // IEs MSGIE_UINT32 ueId; MSGIE_String configFileName; }; </code></pre> <p>Here MSGIE_UINT32 and MSGIE_String are classes derived from MSGIEBase and therefore their address can be stored in the map defined in base class above. When Der1 is constructed the address of ueId and configFileName is stored in the map. Here if I print the size of map ( through gdb and in the program ) it comes to be 24. [ _M_header = 16, _M_node_count = 4, _M_key_compare = 1, 3 byte padding I suppose ].</p> <p>Till here everything is fine. Now the Der1 object pointer is put inside an event object and the event is post into a queue. The event class looks like:</p> <pre><code>class Event { public: MsgBase* msgPtr; }; </code></pre> <p>A different thread removes the event from the queue, extracts the msgPtr and casts it into Der1 Pointer and this is where the problem starts.</p> <p>Here if I print the size of the map in the program it is 21. That means the address of the next member in the MsgBase class i.e. messageId gets shifted by 3 bytes and so the value of messageId changes completely. (when seen through gdb, the address is still intact and so is the size of the map i.e. 24 ).</p> <p>This is a word alignment issue to the best of my knowledge but why is the memory alignment not consistent in different functions and why does the address of a member of a class chage when the memory to the class has been allocated using new. I am using Linux 2.6.27. , gcc version 4.1.1. , RHEL-4.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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