AMD 250 Computer Hardware User Manual


 
Chapter 2 C and C++ Source-Level Optimizations 19
Software Optimization Guide for AMD64 Processors
25112 Rev. 3.06 September 2005
2.7 Dynamic Memory Allocation Consideration
Dynamic memory allocation—accomplished through the use of the malloc library function in C—
should always return a pointer that is suitably aligned for the largest base type (quadword alignment).
Where this aligned pointer cannot be guaranteed, use the technique shown in the following code to
make the pointer quadword aligned, if needed. This code assumes that it is possible to cast the pointer
to a long.
double *p;
double *np;
p = (double *)malloc(sizeof(double) * number_of_doubles + 7L);
np = (double *)((((long)(p)) + 7L) & (-8L));
Then use np instead of p to access the data. The pointer p is still needed in order to deallocate the
storage.
Application
This optimization applies to:
32-bit software
64-bit software