|
-
October 2nd, 2009, 04:56 AM
#3
Re: InterLocked and align
The function will not perform any automatic alignment for non-aligned data for you.
Non aligned 'int':
Code:
#pragma pack(1) // 1 byte alignment
struct Test
{
char a;
int b;
};
Here the variable 'b' at (base+1) offset to the Test struct. If you do not specify #pragma pack directive, compiler will pack the data as per project settings (either 4-byte, or 8-byte is standard). In that case 'b' would be aligned in 4-byte boundary.
Now assume you have a function, that takes int* or int& as an argument:
Code:
void f(int*);
...
Test t;
t.b=100;
f( &t.b );
In function 'f' you might access/modify the passed variable. The variable (pointer/reference) is NOT properly-aligned and the x86 CPU will align it for you in 4-byte boundary. 64-bit processor will NOT align!
If you pass 't.b' variable to InterlockedXX function, the passed variable (pointer) is NOT aligned properly, and thus the unpredictable behaviour!
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
|