aligning a pointer on DWORD boundaries ??? 
Author Message
 aligning a pointer on DWORD boundaries ???

This seems to be necessary for creating a dialog template in memory.

MSDN shows an example of a fucntion that will do this for me but i really
dont understand how it works and i like to know how things work :)

I know about left/right shifting but i still do not see how this acheives
alignment on a DWORD boundary.

The pertinent code is pasted below i would be grateful if someone could
explain it for me.

Thanks.

LPWORD lpwAlign ( LPWORD lpIn)
{
    ULONG ul;

    ul = (ULONG) lpIn;
    ul +=3;
    ul >>=2;
    ul <<=2;
    return (LPWORD) ul;

Quote:
}



Thu, 25 Sep 2003 13:43:40 GMT  
 aligning a pointer on DWORD boundaries ???
Hi!

Quote:

> This seems to be necessary for creating a dialog template in memory.

> MSDN shows an example of a fucntion that will do this for me but i really
> dont understand how it works and i like to know how things work :)

> I know about left/right shifting but i still do not see how this acheives
> alignment on a DWORD boundary.

> The pertinent code is pasted below i would be grateful if someone could
> explain it for me.

> Thanks.

> LPWORD lpwAlign ( LPWORD lpIn)
> {
>     ULONG ul;

>     ul = (ULONG) lpIn;
>     ul +=3;
>     ul >>=2;
>     ul <<=2;
>     return (LPWORD) ul;
> }

Well, first you have to think in binary and bits. Any value that is not DWORD aligned will have
either bit0 (1) or bit1 (2) or both (3) set. Adding 3 will give you the next highest value who's
slop or remainder will be confined to those first two bits. The >>=2 followed by <<=2 will return
this new value with the first two bits cleared. Thus...

0+3(0000+0011) == 3(0011); >>2;<<2; == 0(0000)
1+3(0001+0011) == 4(0100); >>2;<<2; == 4(0100)
2+3(0010+0011) == 5(0101); >>2;<<2; == 4(0100)
3+3(0011+0011) == 6(0110); >>2;<<2; == 4(0100)
4+3(0100+0011) == 7(0111); >>2;<<2; == 4(0100)
5+3(0101+0011) == 8(1000); >>2;<<2; == 8(1000)

etc...

HTH,

Jeff...
--
Please post all follow-ups to the newsgroup only.



Thu, 25 Sep 2003 16:00:16 GMT  
 aligning a pointer on DWORD boundaries ???
Quote:

> This seems to be necessary for creating a dialog template in memory.

> MSDN shows an example of a fucntion that will do this for me but i really
> dont understand how it works and i like to know how things work :)

> I know about left/right shifting but i still do not see how this acheives
> alignment on a DWORD boundary.

> The pertinent code is pasted below i would be grateful if someone could
> explain it for me.

> Thanks.

> LPWORD lpwAlign ( LPWORD lpIn)
> {
>     ULONG ul;

>     ul = (ULONG) lpIn;
>     ul +=3;
>     ul >>=2;
>     ul <<=2;
>     return (LPWORD) ul;
> }

This is a strange way of doing it, with shifts. Normally you simply null-out the last two
bits (all addresses dword-alighed have their numerical address values with two last bits
reset.) You add 3 so as to make sure your real value is always at or above the actual
address (not below, that is: below would give you unallocated memory <g>.)

Iow, you'd say ul += 3; and then simply

ul &= ~3; (this resetting the two ls bits in the ul.)

Then your address will always be a power of 4, which is what you sought to achieve. If you
still have problems grasping it, I suggest you simply work it out on paper in binary. Do
it for two-three random numbers and watch the bit patterns that result from this
operation. Btw, the same is done to achieve any alignment.

Again, to ascertain that your address is dw-aligned, you must make sure it numerically
ends in two zeroes (talking bits here.) So, you simply null them out, BUT, nulling out two
LS bits will have a side effect of *decreasing* the numerical value of your address
(unless it was already dw-aligned, of course, but that's not always the case.) Suppose you
new'ed a chunk to yourself, and now null out two lsb's. Well, now you have a good
dw-aligned address, but unfortunately it's below the allocated chunk, you can't use this
memory coz it's not yours to use. You need a lowest dw-aligned address WITHIN THE BLOCK OF
MEMORY YOU'RE ALLOCATED. So, what you do is to grab more memory than you need, and then
get the *lowest* dw-aligned address within this block. For that you bump your initial
address by at least one dw-address increment so that you have a sufficient clearance from
the base address and then go back to the nearest dw-aligned address down, which is
achieved by clearing two ls bits--either by shifting back and forth, or, simpler yet, by
clearing them out directly. I'm not sure if this all makes it any clearer though <G>. Go
draw it all on paper and write binary values on the picture, you'll see yourself how it
works.



Fri, 03 Oct 2003 01:07:44 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. DWORD Boundary display problem with DIB

2. ALIGN on BOUNDARIES: MULTI-D ARRAY?

3. Aligning Data to Byte Boundaries

4. Aligning doubles to 8 byte boundaries

5. aligning local data on to 16 byte Boundary

6. Tools to align structures in word boundary?

7. DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout ) ;

8. pointers & lower boundary

9. Re : Interface pointers over process boundaries

10. How to check the boundary of pointer?

11. Serialize int variables: DWORD or not DWORD ?

12. How to check the boundary of a pointer????????

 

 
Powered by phpBB® Forum Software