...
Code Block | ||
---|---|---|
| ||
size_t size = 16; size_t alignment = 1<<12; float *ptr; double *ptr1; ptr = aligned_alloc(align , size); if(align == alignof(ptr1)) { ptr1 = realloc(ptr, size); } |
Implementation Details
Another noncompliant example that has been implemented on the x86_64-redhat-linux platform and compiled with gcc version 4.1.2 is as follows:
Code Block |
---|
#include<stdlib.h>
#include<stdio.h>
int main() {
size_t size = 10;
size_t align = 1 << 12;
float *ptr;
double *ptr1;
posix_memalign((void **)&ptr, align , 4);
if((ptr1 = realloc((int *)ptr, size)) == NULL) {
exit(0);
}
ptr[0] = 12.5;
ptr[1] = 25.5;
printf("memory aligned to %d bytes\n\n",align);
printf("ptr[0] : %p = %f\n",ptr, ptr[0]);
printf("ptr[1] : %p = %f\n\n",&ptr[1], ptr[1]);
printf("After realloc(): \n");
printf("ptr1[0] : %p = %f\n",ptr1, ptr[0]);
printf("ptr1[1] : %p = %f\n\n",&ptr1[1], ptr1[1]);
printf("The value at ptr[1] remains the same after realloc()\n");
printf("ptr[1] : %p = %f\n",((float *)ptr1+1),*((float *)ptr1+1));
free(ptr1);
return 0;
}
|
The noncompliant example produces the following (unexpected) output. The noncompliant codexample produces the following (unexpected) output on the x86_64-redhat-linux platform that was compiled with gcc version 4.1.2.
({{ptr\[0\]}} is initialized to 12.5 and {{ptr\[1\]}} is initialized to 25.5) Wiki Markup
Code Block |
---|
ptr[0] (0x2b7000000000) = 12.500000 ptr[1] (0x2b7000000004) = 25.500000 ptr1[0] (0x2b7000000000) = 12.500000 ptr1[1] (0x2b7000000008) = 0.000000 |
...