util.c 588 B

123456789101112131415161718192021222324252627282930313233
  1. /* Name: util.c
  2. * Author: Cecylia Bocovich <cbocovic@uwaterloo.ca>
  3. *
  4. * This file contains helper functions and macros
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "util.h"
  9. //malloc macro that exits on error
  10. void *emalloc(size_t size){
  11. void *ptr = malloc(size);
  12. if (ptr == NULL){
  13. fprintf(stderr, "Memory failure. Exiting...\n");
  14. exit(1);
  15. }
  16. return ptr;
  17. }
  18. //calloc macro that exits on error
  19. void *ecalloc(size_t nmemb, size_t size){
  20. void *ptr = calloc(nmemb, size);
  21. if(ptr == NULL){
  22. fprintf(stderr, "Memory failure. Exiting...\n");
  23. exit(1);
  24. }
  25. return ptr;
  26. }