#include <stdio.h>
#include <stdlib.h>
#include <string> int main()
{
char * p_char; //you should include <stdlib.h> - because that is where malloc() is declared
p_char=(char *)malloc(); int count();
count=count<<;
printf("count=%d",count);
//任务管理看到内存不断增大
//while(count>0)
//{
// p_char=(char *)malloc(100);
// count--;
//} //#include <string> char * strcpy ( char * destination, const char * source );
strcpy(p_char,"Hello");
if(p_char)
//you should include <stdio.h>
printf("Memory allocated at %x\n",p_char);
else
printf("Not Enough Memory!\n"); char * p_char_next;
p_char_next=p_char; p_char=(char *)realloc(p_char,); //任务管理看到内存没有变化
while(count>)
{
p_char=(char *)realloc(p_char,);
count--;
} if(p_char)
printf("Memory Reallocated at %x\n",p_char);
else
printf("Not Enough Memory!\n"); printf("Source Address at %x\n",p_char_next);
//printf("Content Of Source is %s,\nContent Of Current is %s\n",*p_char,*p_char_next); free(p_char);
getchar();
return ;
};
realloc
原型:extern void *realloc(void *mem_address, unsigned int newsize);
用法:#include <stdlib.h> 有些编译器需要#include <alloc.h>
功能:改变mem_address所指内存区域的大小为newsize长度。
说明:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。
详细说明及注意要点:
1、如果有足够空间用于扩大mem_address指向的内存块,则分配额外内存,并返回mem_address
这里说的是“扩大”,我们知道,realloc是从堆上分配内存的,当扩大一块内存空间时, realloc()试图直接从堆上现存的数据后面的那些字节中获得附加的字节,如果能够满足,自然天下太平。也就是说,如果原先的内存大小后面还有足够的空闲空间用来分配,加上原来的空间大小= newsize。那么就ok。得到的是一块连续的内存。
2、如果原先的内存大小后面没有足够的空闲空间用来分配,那么从堆中另外找一块newsize大小的内存。
并把原来大小内存空间中的内容复制到newsize中。返回新的mem_address指针。
 
//free(p_char_next);
if(p_char_next!=NULL)
printf("p_char_next = %x.\n",p_char_next);
Attention:You cann't free p_char_next again.
 
1、 返回void * 指针,调用成功。
2、 返回NULL,当需要扩展的大小(第二个参数)为0并且第一个参数不为NULL,此时原内存变成了“freed(游离)”的了。
3、 返回NULL,当没有足够的空间可供扩展的时候,此时,原内存空间的大小维持不变。

最新文章

  1. KOTLIN开发语言文档(官方文档) -- 2.基本概念
  2. H5移动APP开发 细节详解(转)
  3. python opencv 实现Reinhard颜色迁移算法
  4. js中的json
  5. trap
  6. 周爱民-javascript
  7. 关于懒加载(lazy loading)
  8. 自己定义actionbar
  9. 对Git的一些理解
  10. Beautifulsoup 和selenium 的查询
  11. cacti监控系统
  12. 201521123014 《Java程序设计》第14周学习总结
  13. 二:Redis快速入门及应用
  14. SQLyog键盘快捷方式
  15. Django的model form组件
  16. 3 The simple past
  17. Navicat导入sql server数据库
  18. 一切为了落地,为什么要把PP.io设计成三个阶段!
  19. Thinkphp语句拼接
  20. 更改mac系统语言及其软件

热门文章

  1. 3D Studio Max [www]
  2. golang锁记
  3. caffe Python API 之卷积层(Convolution)
  4. aspxgridview export导出数据,把true显示成‘是’
  5. php设计模式三-----建造者模式
  6. POJ 2912 Rochambeau(种类并查集+枚举)
  7. Go语言标准库之log包
  8. Partial Views
  9. ubuntu上安装mysql及导入导出
  10. 【剑指offer】面试题 15. 二进制中 1 的个数