Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC
langc
#include <assert.h>
 
void func(void) {
  char c = 'x';
  int *ip = (int *)&c; /* This can lose information. */
  char *cp = (char *)ip;
  assert(cp == &c);    /* Will fail on some conforming implementations */

  /* ... */
}

...

Code Block
bgColor#FFCCCC
langc
#include <assert.h>
 
void func(void) {
  char c = 'x';
  int *ip = (int *)&c; /* This can lose information. */
  char *cp = (char *)ip;
  assert(cp == &c);    /* Will fail on some conforming implementations */

  /* ... */
}

...

Code Block
bgColor#ccccff
langc
#include <stdalign.h>  /* For alignas() */
#include <assert.h>
 
void func(void) {
  alignas(int) char c = 'x'; /* Align c to the alignment of an int */
  int *ip = (int *)&c; 
  char *cp = (char *)ip;
  assert(cp == &c);    /* Both cp and &c point to equally aligned objects. */
  /* ... */
}

Risk Assessment

...