Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: MEM02-C compliance

...

Code Block
bgColor#ffcccc
langc
#include <stdlib.h>
 
void func(void) {
  size_t resize = 1024;
  size_t alignment = 1 << 12;
  int *ptr;
  int *ptr1;

  if ((ptr = (int*) aligned_alloc(alignment, sizeof(int))) == NULL) {
    /* Handle error */
  }

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

...

Code Block
#include <stdlib.h>
#include <stdio.h>

int main(void) {
  size_t  size = 16;
  size_t resize = 1024;
  size_t align = 1 << 12;
  int *ptr;
  int *ptr1;

  if (posix_memalign((void **)&ptr, align , size) != 0) {
    exit(EXIT_FAILURE);
  }

  printf("memory aligned to %zu bytes\n", align);
  printf("ptr = %p\n\n", ptr);

  if ((ptr1 = (int*) realloc((int *)ptr, resize)) == NULL) {
    exit(EXIT_FAILURE);
  }

  puts("After realloc(): \n");
  printf("ptr1 = %p\n", ptr1);

  free(ptr1);
  return 0;
}

...

Code Block
bgColor#ccccff
langc
#include <stdlib.h>
#include <string.h>
 
void func(void) {
  size_t resize = 1024;
  size_t alignment = 1 << 12;
  int *ptr;
  int *ptr1;

  if ((ptr = (int*) aligned_alloc(alignment, sizeof(int))) == NULL) {
    /* Handle error */
  }

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

  if ((memcpy(ptr1, ptr, sizeof(int)) == NULL) {
    /* Handle error */
  }

  free(ptr);
}

...

Code Block
bgColor#ccccff
langc
#include <malloc.h>
 
void func(void) {
  size_t alignment = 1 << 12;
  int *ptr;
  int *ptr1;
 
  /* Original allocation */
  if ((ptr = (int*) _aligned_malloc(sizeof(int), alignment))
      == NULL) {
    /* Handle error */
  }
 
  /* Reallocation */
  if ((ptr1 = (int*) _aligned_realloc(ptr, 1024, alignment))
      == NULL) {
    _aligned_free(ptr);
    /* Handle error */
  }

  _aligned_free(ptr1);
}

...