Versions Compared

Key

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

...

Code Block
bgColor#ffcccc
size_t size = 16;
size_t resize = 1024;
size_t alignment = 1 << 12;
int *ptr;
int *ptr1;

if((ptr = aligned_alloc(alignment , sizesizeof(int))) == NULL) {
  /* handle error */
}

if((ptr1 = realloc(ptr, resize)) == NULL) {
  /* handle error */
}

...

This compliant solution implements an aligned realloc() function. It allocates new memory of resize bytes with an alignment equal to that of old memory and copies old memory into it. It then frees the old memory.

Code Block
bgColor#ccccff

size_t size = 16;
size_t resize = 1024;
size_t alignment = 1 << 12;
size_t newsize;
int *ptr;
int *ptr1;

if((ptr = aligned_alloc(alignment , sizesizeof(int))) == NULL) {
  /* handle error */
}

if((ptr1 = aligned_alloc(alignment , resize)) == NULL) {
  /* handle error */
}

newsize = MIN(size, resize);
if((memcpy(ptr1, ptr, newsize) == NULL) {
 /* handle error */
}

free(ptr);

...