D
Denis Remezov
__PPS__ said:Actually what I mean is that - if I have some memory buffer, lets say
char a[64]; and then I do like this:
DWORD num = 0x1234;
*(DWORD*)a = num; (1)
*(DWORD*)(a+1) = num; (2)
either (1) or (2) will assign dword value to not dword aligned
address. The question is - will this code be fatal on some systems??
I used it in a program and somebody told me that it produces fatal
bugs for some systems . From language point of view this staement is
perfectly legal, so I'm wondering is it a real problem???
For this type of conversion (reinterpret_cast for data pointers) the
standard makes only one guarantee: the result of a second convertion to
the original pointer type is the original pointer value (and only
provided that the alignment requirements for unsigned long are the
same (for this example) as for char). The result of anything that
you do beyond that is unspecified, i.e. implementation-dependent.
On some systems, misaligned data access will cause a fatal error.
You may be able to enable system-specific traps to handle misaligned
addresses at the cost of the performance. On some others, there
are no traps and no errors but the performance will still suffer.
Sometimes noticeably. It's all system-specific.
The standard explicitly permits to copy a POD object (that includes
built-in types) into a char array and then copy the contents back
into the object, character by character (e.g. by using memcpy).
If what you read from the array is garbage, the behaviour, I suppose,
is undefined (not all possible bit combinations need to represent
a valid object value, in principle; unfortunately, I don't remember
seeing examples for integral types).
(By the way, it's better to avoid using non-standard definitions
such as DWORD for the purpose of discussion here. What I managed
to grep on my system might be even slightly different from
your DWORD ).
Denis