c_cpp C中的分配实践
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 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;
}
以上是关于c_cpp C中的分配实践的主要内容,如果未能解决你的问题,请参考以下文章