While it used to be common practice to use integers and pointers interchangeably in C (although it was not considered good style), the C99 standard has mandated that pointer to integer and integer to pointer conversions are implementation defined. By definition of the standard, the only value which can be considered interchangeable between pointers and integers is the constant 0. This means that they are not necessarily portable from one system to the next, and additionally multiple conversions may or may not give the desired behaviorless the exception of 0, mixing integers and pointers may have undesired consequences depending on the implementation which is used.
Universal Integer/Pointer Storage
It is recommended to use a union if you need to memory to be accessible as both a pointer and an integer rather than make the cast. Since a union is the size of the largest element and will faithfully represent both as the implementation defines, it will ensure the proper behavior and keep data from being lost.
Non-Compliant Code
Non-Compliant Code
In this non-compliant code, the pointer ptr
is used in an arithmetic operation that is eventually casted as an integer, as stated above, the actual result for both this assignment and following assignment to ptr2
are implementation defined.
Code Block | ||
---|---|---|
| ||
unsigned int *ptr = 0xcfcfcfcf; ... unsigned int number = ptr + 1; unsigned int *ptr2 = ptr; |
Compliant Solution
A union can be used to give raw memory access to both an integer and a pointer and will take up as much size as its largest element which will mitigate a loss of data.
Code Block | ||
---|---|---|
| ||
union intpoint { unsigned int *pointer; unsigned int number; } intpoint; ... intpoint mydata = 0xcfcfcfcf; ... unsigned int num = mydata.number + 1; unsigned int *ptr = mydata.pointer; |
The non-compliant code leads to many possible conversion errors and additionally overflow. All of these are avoided by using a union.
Accessing Memory-Mapped Addresses*
Note: This is a bad idea. The following is a description of how to properly execute a bad idea.
Often times Oftentimes in low level , kernel , and or graphics code you will need to access memory at a specific locationslocation. This means Thus, you are going to have to make a literal integer to pointer to conversion! This will make your code non-portable!
The trick here is to convince the compiler you are going to do something bad instead of just assigning a pointer to an integer.
...
.
Non-Compliant Code Example
In this non-compliant code, a pointer is set directly to an integer constant, where it is unknown whether the result will be as intended.
Code Block | ||
---|---|---|
| ||
unsigned int *ptr = 0xcfcfcfcf; |
Because integers are no longer pointers this could have drastic consequences.
Compliant Solution
...
bgColor | #ccccff |
---|
...
The trick here is you are telling the compiler you really do want to do this bad thing. If on your system pointers are typecast to something other than an unsigned int, then you want to use the type they are defined to be instead of unsigned int.
Adding in the explicit cast may help the compiler make a decision that
Code Block | ||
---|---|---|
| ||
unsigned int * ptr = (unsigned int *) 0xcfcfcfcf;
|
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
INT14-A | 2 | 2(unlikely) | 3(medium) | P2 | L3 |
Credits:
- I used this little tutorial to help me figure out the particulars of unions, as they are not often used. Read it here
...