Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC
char *charloop_ptr = "example";
int * int_ptr;

int *voidPtr2intPtrmkVoidPtr(void *v_pointer){

  return v_pointer;
}
int_ptr = voidPtr2intPtrmkVoidPtr(charloop_ptr);

In this example the v_pointer might be aligned on a 1 byte boundary. Once it is cast to an int some architectures will require it to be on 4 byte boundaries. Pointers are often cast because a void* cannot be dereferenced. Careless coding can result in an arbitrary pointer type being used irregardless of its alignment.

...

Type

Alignment

char

1 byte aligned

short

2 byte aligned

int

4 byte aligned

float

4 byte aligned

double

8 byte on Windows, 4 byte on Linux

Compliant Solution

...

...

-Make

...

specific

...

functions

...

(avoid

...

use

...

of

...

void*)

...

-Always

...

use

...

strictest

...

alignment

...

type

...

for

...

arbitrary pointers.

Code Block

int *loop_ptr;
int * int_ptr;

int *loopFunction(int *v_pointer){

return v_pointer;
}
int_ptr = loopFunction(loop_ptr);

pointers.
Code Block
bgColor#ccccff

Risk Assessment

Accessing a pointer that is no longer on the correct access boundary can cause a program to crash, give wrong information or have slow pointer accesses (if the architecture does not care about alignment).

...