Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3.  
  4. //c program illustrating construction vs allocation
  5.  
  6. //sample container struct
  7. typedef struct {
  8. int type;
  9. void* data;
  10. } Obj;
  11.  
  12. void drop_int(Obj* o, const int i)
  13. {
  14. o->type = i;
  15. }
  16.  
  17. int main(void) {
  18. Obj* f = (Obj*)malloc(sizeof(Obj)*3);
  19. //assignable after creation
  20. f->type = 3;
  21. f++;
  22. f->type = 5;
  23. f--;
  24. int am = 8888;
  25. drop_int(f, am);
  26. printf("the type is %d\n", f->type);
  27. unsigned int h = 6;
  28. printf("this is unsigned %d\n", sizeof(unsigned int));
  29. free((void*)f);
  30. Obj g = {2, malloc(5)};
  31. Obj* j = &g;
  32. free(j->data);
  33. //null assignable after free
  34. j->data = NULL;
  35. printf("is null: %d\n", j->data == NULL);
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement