jweinst1
6/27/2017 - 10:35 PM

allocation practice in C

allocation practice in C

#include "stdio.h"
#include "stdlib.h"

//c program illustrating construction vs allocation

//sample container struct
typedef struct {
  int type;
  void* data;
} Obj;

void drop_int(Obj* o, const int i)
{
  o->type = i;
}

int main(void) {
  Obj* f = (Obj*)malloc(sizeof(Obj)*3);
  //assignable after creation
  f->type = 3;
  f++;
  f->type = 5;
  f--;
  int am = 8888;
  drop_int(f, am);
  printf("the type is %d\n", f->type);
  unsigned int h = 6;
  printf("this is unsigned %d\n", sizeof(unsigned int));
  free((void*)f);
  Obj g = {2, malloc(5)};
  Obj* j = &g;
  free(j->data);
  //null assignable after free
  j->data = NULL;
  printf("is null: %d\n", j->data == NULL);
  return 0;
}