Advertisement
ikegami

Untitled

Jan 14th, 2021
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1.     char *str = NULL;
  2.     size_t len = 0;
  3.     size_t allocd = 0;
  4.  
  5.     size_t n;
  6.     char buf[SIZ];
  7.     while ((n = fread(buf, 1, sizeof(buf), stdin)))
  8.     {
  9.         allocd += n;
  10.         str = realloc(str, allocd);   // Extend the buffer
  11.         memcpy(str+len, buf, n);      // Append to the string
  12.         len += n;
  13.     }
  14.  
  15.     ++allocd;
  16.     str = realloc(str, allocd);
  17.     str[len] = 0;
  18.  
  19.     printf("ALLOC'D: %zu", allocd);
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement