Versions Compared

Key

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

...

Code Block
int *dat = (int *)malloc(ARRAY_SIZE * sizeof(int));

Always check that malloc() returns a non-NULL pointer, as per MEM32-C. Detect and handle memory allocation errors.

It is important to retain any pointer value returned by malloc() so that the referenced memory may eventually be deallocated. One possible way of preserving such a value is to use a constant pointer.

Code Block
int * const dat = (int * const)malloc(
  ARRAY_SIZE * sizeof(int)
);
/* ... */
free(dat);
dat = NULL;

Both dis and dat arrays can be initialized as follows:

...