设为首页 加入收藏

TOP

C语言内存分配函数malloc(二)
2014-04-06 17:42:27 来源: 作者: 【 】 浏览:419
Tags:语言 内存 分配 函数 malloc

 

  //print the allocated address

  printf("The address get by realloc is : %p\n",new_ptr);

  //print the 4 integers at the beginning

  printf("4 integers at the beginning is:\n");

  for (i=0;i<4;i++)

  {

  printf("%d\n",new_ptr[i]);

  }

  return 0;

  }

  运行结果如下:

C语言内存分配函数malloc、calloc和realloc

  从上面可以看出,在这个例子中新的空间并不是以原来的空间为基址分配的,而是重新分配了一个大的空间,然后将原来空间的内容拷贝到了新空间的开始部分。

  3、三者的联系

  calloc(n,size)就相当于malloc(n*size),而realloc(*ptr,size)中,如果ptr为NULL,那么realloc(*ptr,size)就相当于malloc(size)。

      

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇1.3.2 C语言标准 下一篇关于C语言中二维数组传参

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: