动态内存分配问题
# include <stdio.h># include <malloc.h>
void f(int ** q)
{
*q = (int *)malloc(5);
*(*q+1) = '5';
}
int main(void)
{
int * p;
f(&p);
printf("%c\n", *(p+1));
free(p);
return 0;
}
大家看看红色的这几行代码有什么问题么,我的用意是把5用一个字节来存。
# include <stdio.h> # include <malloc.h> void f(int **q) { *q = (int *)malloc(5); *(char *)(*q+1) = '5'; //force to char *, don't destory hcb(heap control block) structure } int main(void) { int * p; f(&p); printf("%c\n", *(p+1)); free(p); return 0; }