|
-
November 21st, 2009, 11:59 AM
#1
[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
-
November 21st, 2009, 12:12 PM
#2
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.
-
November 21st, 2009, 01:37 PM
#3
Re: Aligning pointers to struct
 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?
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...
-
November 22nd, 2009, 04:12 AM
#4
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
-
November 22nd, 2009, 05:28 AM
#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.
-
November 22nd, 2009, 06:59 AM
#6
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
-
November 22nd, 2009, 09:38 AM
#7
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
-
November 22nd, 2009, 10:07 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|