Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This example compiles without warning. However, v_pointer may be aligned on a one-byte boundary. Once it is cast to an int *, some architectures will require that the object is aligned on a four-byte boundary. If int_ptr is later dereferenced, the program may terminate abnormally. Simply casting to int * may cause a loss of information, even if the value is not dereferenced. For example, the following code is not guaranteed to work conforming C99 implementations, even though no pointers are dereferenced:

Code Block
bgColor#FFCCCC

char c = 'x';
int *p = (int*) &c; /* this can lose information */
char *p2 = (char*) p;
assert(p2 == &c); /* will fail on some conforming implementations */

On some implementations p2 will not match &c.

One solution is to ensure that loop_ptr points to an object returned by malloc() because this object is guaranteed to be aligned properly for any need. However, this is a subtlety that is easily missed when the program is modified in the future. It is cleaner to let the type system document the alignment needs.

...