10_malloc.c 448 B

123456789101112131415161718192021
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main()
  5. {
  6. char *mem;
  7. mem = (char *)malloc(16);
  8. memset(mem, 'A', 15);
  9. mem[15] = 0;
  10. printf("Dynamically allocated memory at %p\n", mem);
  11. printf("Content: %s\n", mem);
  12. mem = (char *)realloc(mem, 32);
  13. memset(mem, 'A', 31);
  14. mem[31] = 0;
  15. printf("Reallocated, new address is %p\n", mem);
  16. printf("Content: %s\n", mem);
  17. free(mem);
  18. printf("Freed memory at %p\n", mem);
  19. return 0;
  20. }