fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct s {
  4. int i;
  5. };
  6. int main(void){
  7. struct s *p1 = malloc(sizeof(struct s));
  8. p1->i = 42;
  9. printf("(p1) before free: p->i = %d\n", p1->i);
  10. free(p1);
  11. struct s *p2 = malloc(sizeof(struct s));
  12. p2->i = 100;
  13. // Trying to use the dangling pointer p1
  14. printf("(p1) after free: p->i = %d\n", p1->i);
  15. printf("(: p2->i = %d\n", p2->i);
  16. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
(p1) before free: p->i = 42
(p1) after free: p->i = 0
(: p2->i = 100