CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2005
    Posts
    91

    [RESOLVED] Aligning pointers to struct

    Hi,
    I have two structs and I would like to assign a pointer from t_TMSG to an address in t_TAG.
    But the problem is i dont get the correct address.

    typedef struct {
    WORD tag;
    WORD opr;
    DWORD unit;
    LPCTSTR text;
    } t_TAG;

    typedef struct {
    long tag;
    LPCTSTR text;
    } t_TMSG;


    t_TAG* pTag = SomeFunction(); //assuming pTAg is loaded with values
    t_TMSG *pTMsg = (t_TMSG*)(pTag + sizeof(WORD) + sizeof(WORD) ); // i get the wrong address here.


    Please help

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Aligning pointers to struct

    Probably because of default alignment (8 byte boundaries by default I think). Try wrapping your structure definitions in "#pragma pack" directives:

    Code:
    #pragma pack (push,1)
    typedef struct {
    WORD tag;
    WORD opr;
    DWORD unit;
    LPCTSTR text;
    } t_TAG;
    
    typedef struct {
    long tag;
    LPCTSTR text;
    } t_TMSG;
    
    #pragma pack(pop)
    
    t_TAG* pTag = SomeFunction(); //assuming pTAg is loaded with values
    t_TMSG *pTMsg = (t_TMSG*)(pTag + sizeof(WORD) + sizeof(WORD) ); // i get the wrong address here.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Aligning pointers to struct

    Quote Originally Posted by dacky View Post
    t_TMSG *pTMsg = (t_TMSG*)(pTag + sizeof(WORD) + sizeof(WORD) ); // i get the wrong address here.
    According to the rules of pointer arithmetic, pTag + 1 points to the NEXT struct t_TAG, effectively adding a sizeof(t_TAG) to the pointer’s value.
    Is that your intention?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Dec 2005
    Posts
    91

    Re: Aligning pointers to struct

    @hoxsiew
    hi I tried your idea of the #pragma directive but it didn't work, the pointer jumped like 48 bytes.
    And when i tried to just add 1 to the pointer
    t_TMSG *pTMsg = (t_TMSG*)(pTag+1); // this will jump 12 bytes, but 8 bytes before without pragma
    any more ideas? thanks

    @VladimirF
    Yes, you are correct when i do pTag + 1, it jumps to 8 bytes and i think it is the size of t_TMSG.
    I want to the address of pTag->unit to be in pTMsg. Is this really possible?

    Thanks

  5. #5

    Re: Aligning pointers to struct

    Of course you get the wrong address, what you are doing makes no sense because the types inside are different. You might want to use a void* instead, or it could be what you want is not something valid in the first place. You should probably say what you are really trying to achieve if you want more help.

  6. #6
    Join Date
    Dec 2005
    Posts
    91

    Re: Aligning pointers to struct

    what do you mean no sense? does it make any sense to align pTag->unit to pTMsg? The types may be different but they have the same size. What I am trying to achieve is that I only need pTag->unit and pTag-txt and I want starting from the address of pTag->unit to point to pTMsg which has the same allocated memory size of pTag->unit and pTag-txt. I hope this is clear enough. thanks hope you can help

  7. #7
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Aligning pointers to struct

    How about trying this:
    Code:
    t_TMSG *pTMsg = (t_TMSG*)(&pTag->unit);
    This assumes you want pTMsg to point to the unit member.

    Although in your definitions, you have unit as a DWORD, and in the t_TMSG structure it's a long (signed/unsigned).

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  8. #8
    Join Date
    Dec 2005
    Posts
    91

    Re: Aligning pointers to struct

    wow it works!!!
    thanks krmed!!! why didn't i think of that...i have the correct address now..thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured