|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- /*#include<malloc.h>*/
- #include<stdlib.h>
- int main()
- {
- int i ,* temp;
- const int size = 20;
- int *p = (int *)malloc(5 * sizeof(int));
- temp=p;
- printf("malloc后指向地址%x\n", temp);
- for (i=0;i<5;temp++,i++)
- {
- *temp=0xaabbccdd;
- }
- temp=p;
- i=0;
- printf("p[5]内容%x ", *temp);
- temp++;
- for (i=1;i<5;temp++,i++)
- {
- printf("%x ", *temp);
- }
- temp=p;
- int *p1 = (int *)realloc(p, size*sizeof(int));
- printf("\nrealloc后p指向地址%x\n", p);
- i=0;
- printf("p[5]内容%x ", *temp);
- temp++;
- for (i=1;i<5;temp++,i++)
- {
- printf("%x ", *temp);
- }
- temp=p1;
- printf("\nrealloc后p1指向地址%x\n", p1);
- i=0;
- printf("p1[10]内容%x ", *temp);
- temp++;
- for (i=1;i<10;temp++,i++)
- {
- printf("%x ", *temp);
- }
- // free(p); 在 int *p1 = (int *)realloc(p, size*sizeof(int)); 如果执行该语句会编译出错,由于地址已经改变,原来的p指向空间再执行realloc时已经回收
- free(p1);
- system("pause");
- return 0;
- }
复制代码
|
-
运行调试截图
|