fork(1) download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <stdbool.h>\
  7. \\#include <crtdbg.h>//VC only...
  8.  
  9. typedef struct Memory{
  10. void* Memory;
  11. size_t Length;
  12. size_t ESize;
  13. }Memory;
  14.  
  15. Memory ConstructMemory(size_t Length, size_t ElementSize){
  16. Memory M={ 0, };
  17.  
  18. M.Memory = calloc(Length, ElementSize);
  19. if (M.Memory == NULL) return M;
  20.  
  21. M.Length = Length;
  22. M.ESize = ElementSize;
  23.  
  24. return M;
  25. }
  26.  
  27. bool ResizeMemroy(Memory* M, size_t Length){
  28. void *T = NULL;
  29.  
  30. T= realloc(M->Memory, Length*M->ESize);
  31. if (errno == ENOMEM) return false;
  32. if (T != M->Memory)M->Memory = T;
  33. M->Length = Length;
  34.  
  35. return true;
  36. }
  37.  
  38.  
  39. bool AppendMemory(Memory* M, size_t Add){
  40. return ResizeMemroy(M, M->Length + Add);
  41. }
  42.  
  43. bool IsMemoryNull(Memory* M){
  44. return M->Memory == NULL;
  45. }
  46.  
  47. void DestructMemroy(Memory* M){
  48. free(M->Memory);
  49. M->Memory = NULL;
  50. M->Length = 0;
  51. M->ESize = 0;
  52. }
  53.  
  54. int main(){
  55. char F[] = "I am ";
  56. char L[] = "Bob.";
  57.  
  58. Memory M = ConstructMemory(strlen(F)+1, sizeof(char));
  59. if (IsMemoryNull(&M) == true) exit(1);
  60.  
  61. strcat(M.Memory, F);
  62.  
  63. printf("Memory is '%s'\n", M.Memory);
  64.  
  65. if (AppendMemory(&M, strlen(L)) == false) {
  66. DestructMemroy(&M);
  67. exit(1);
  68. }
  69. strcat(M.Memory, L);
  70.  
  71. printf("Memory is '%s'\n", M.Memory);
  72.  
  73. DestructMemroy(&M);
  74.  
  75. printf("Done!\n");
  76.  
  77. //_CrtCheckMemory();//vc only...
  78. return 0;
  79.  
  80. }
Success #stdin #stdout 0s 2140KB
stdin
Standard input is empty
stdout
Memory is 'I am '
Memory is 'I am Bob.'
Done!