I am going through the following link to understand memory alignment: https://www.ibm.com/developerworks/library/pa-dalign/#N10159. However I am not able to understand the code snippet given below.
void Munge8( void *data, uint32_t size ) {
uint8_t *data8 = (uint8_t*) data;
uint8_t *data8End = data8 + size;
while( data8 != data8End ) {
*data8++ = -*data8;
}
}
Here the intent is to increment the pointer and that could have been done by "data8 = data8 + 1" but the code in question uses "*data8++ = -*data8". Both of them work fine i.e. increment the pointer but I am unable to understand the logic behind the later. Is it better than "data8 = data8+1"?
During compilation I get an error "alignment_test1.c:44: warning: operation on ‘data8’ may be undefined".
The second part of the question is regarding the code snippet below (from the same link mentioned earlier).
Listing 2. Munging data two bytes at a time
void Munge16( void *data, uint32_t size ) {
uint16_t *data16 = (uint16_t*) data;
uint16_t *data16End = data16 + (size >> 1); /* Divide size by 2. */
uint8_t *data8 = (uint8_t*) data16End;
uint8_t *data8End = data8 + (size & 0x00000001); /* Strip upper 31 bits. */
while( data16 != data16End ) {
*data16++ = -*data16;
}
while( data8 != data8End ) {
*data8++ = -*data8;
}
}
What could be the reason behind the second 'while' loop? Because data8 and data8End are always going to be same in this case.