fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define STRINGIZE(x) #x
  6. #define LINEFILE(x) "Line " STRINGIZE(x) " of File " __FILE__
  7. #define LINE_FILE LINEFILE(__LINE__)
  8.  
  9. /* Comment out the next line to disable debugging diagnostics. */
  10. #define DEBUG_D
  11. #ifdef DEBUG_D
  12. /* Debugging functions and variables. */
  13. static unsigned long malloc_b = 0;
  14. static unsigned long malloc_c = 0;
  15. static unsigned long free_c = 0;
  16.  
  17. static inline void* dmalloc(size_t size);
  18. static inline void dfree(void* object);
  19. static inline void dprintc(void);
  20.  
  21. static inline void* dmalloc(size_t size)
  22. {
  23. malloc_b += size;
  24. ++malloc_c;
  25. return malloc(size);
  26. }
  27.  
  28. static inline void dfree(void* object)
  29. {
  30. free(object);
  31. ++free_c;
  32. }
  33.  
  34. static inline void dprintc(void)
  35. {
  36. fprintf(stderr, "\nAllocated %lu bytes; malloc() called: %lu.\nfree() called: %lu.\n",
  37. malloc_b, malloc_c, free_c);
  38. }
  39.  
  40. #define malloc dmalloc
  41. #define free dfree
  42.  
  43. #endif /* ifdef DEBUG_D */
  44.  
  45. static inline void swap(void* A, void* B, size_t size);
  46.  
  47. int main(void)
  48. {
  49. unsigned long long mb;
  50. while (malloc(sizeof(char) << 20)) ++mb;
  51. printf("Allocated %llu Kilobytes Total.\n", mb);
  52. dprintc();
  53. return 0;
  54. }
  55.  
  56. static inline void swap(void* A, void* B, size_t size)
  57. {
  58. char* temp;
  59. temp = malloc(size);
  60. if (temp)
  61. {
  62. memcpy(temp, A, size);
  63. memcpy(A, B, size);
  64. memcpy(B, temp, size);
  65. free(temp);
  66. }
  67. }
Success #stdin #stdout 0.01s 261824KB
stdin
Standard input is empty
stdout
Allocated 577733508809539825 Kilobytes Total.