...
If the misaligned pointer is dereferenced, the program may terminate abnormally. The cast alone 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 |
---|
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 |
---|
|
char *loop_ptr;
int *int_ptr;
int *loop_function(void *v_pointer) {
/* ... */
return v_pointer;
}
int_ptr = loop_function(loop_ptr);
|
...
Code Block |
---|
|
int *loop_ptr;
int *int_ptr;
int *loop_function(int *v_pointer) {
/* ... */
return v_pointer;
}
int_ptr = loop_function(loop_ptr);
|
...
Code Block |
---|
|
char *data;
struct foo_header *tmp;
struct foo_header *header;
tmp = data + offset;
memcpy(&header, tmp, sizeof(header));
if (header.len < FOO)
/* ... */
|
...
Code Block |
---|
|
char *data;
struct foo_header header;
memcpy(&header, data + offset, sizeof(header));
if (header.len < FOO)
/* ... */
|
...
Tool | Version | Checker | Description |
---|
| | | |
| | | Section |
---|
Can detect some violations of this rule when the -Wcast-align flag is used. |
|
Section |
---|
EDG Front End to Compass/ROSE |
| | | |
| | | Section |
---|
Can detect violations of this rule. However, it does not flag explicit casts to void * and then back to another pointer type. |
|
| | | |
...