[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
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.
Re: Aligning pointers to struct
Quote:
Originally Posted by
dacky
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?
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
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.
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
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.
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