qfedu-c-level/day10/d15.c

18 lines
591 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// malloc(30) 申请大小为 30 字节的内存空间,返回的是这块内存空间的首地址
char *p = (char *)malloc(30);
strcpy(p, "hi, disen");
printf("%s, p->%p, last p->%p\n", p, p, p + 8);
free(p); // free 后,变为野指针,指向的内存空间已经被释放了
// free 之后p 指针指向的内存空间已经被释放了,但是 p 指针本身的地址没有变
p = (char *)ralloc(p, 10);
memset(p, 'A', 10);
printf("%s, p->%p\n", p, p);
return 0;
}