动态内存分配问题
# 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用一个字节来存。
2012-01-14 23:54
2012-01-14 23:57
2012-01-15 02:58

2012-01-15 06:12
程序代码:# 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;
}

2012-01-15 09:38
2012-01-16 12:11

2012-01-16 12:38
2012-01-16 14:37

2012-01-16 15:31
2012-01-16 15:42