fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct
  6. {
  7. char* location;
  8. int temp;
  9. int numberOfRec;
  10. }TEMP;
  11.  
  12. // Your wrapper functions
  13. void *xCalloc(size_t numElements, size_t sizeofElement)
  14. {
  15. return calloc(numElements, sizeofElement);
  16. }
  17.  
  18. void *xRealloc(void *ptr, size_t newSize)
  19. {
  20. return realloc(ptr, newSize);
  21. }
  22.  
  23. void someFunction(TEMP* data)
  24. {
  25. void *temp;
  26. temp = xRealloc (data->location, 10 * sizeof(char));
  27. if(temp == NULL)
  28. {
  29. // Ran out of memory
  30. exit(EXIT_FAILURE);
  31. }
  32.  
  33. data->location = temp;
  34. strncpy(data->location, "Hello", 6); // 6 = 5 + 1 (for NULL terminator)
  35. }
  36.  
  37. int main(void)
  38. {
  39. TEMP* data = xCalloc (1, sizeof(TEMP));
  40. data->location = xCalloc (20, sizeof(char));
  41. printf("Before realloc: %d\n", strlen(data->location));
  42. someFunction(data);
  43. printf("After realloc: %d\n", strlen(data->location));
  44.  
  45. if(data->location != NULL)
  46. free(data->location);
  47. if(data != NULL)
  48. free(data);
  49. return 0;
  50. }
Success #stdin #stdout 0s 1964KB
stdin
Standard input is empty
stdout
Before realloc: 0
After realloc: 5