• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

学习c/c++之realloc(仅供参考)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 
realloc
 
void * realloc ( void * ptr, size_t size );
 

Reallocate memory block

 

    The size of the memory block pointed to by the ptr parameter is changed to the size bytes, expanding or reducing the amount of memory available in the block.

    The function may move the memory block to a new location, in which case the new location is returned. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved. If the new size is larger, the value of the newly allocated portion is indeterminate.

    In case that ptr is NULL, the function behaves exactly as malloc, assigning a new block of size bytes and returning a pointer to the beginning of it.

    In case that the size is 0, the memory previously allocated in ptr is deallocated as if a call to 
free was made, and a NULL pointer is returned.

Parameters

 
ptr
Pointer to a memory block previously allocated with malloccalloc or realloc to be reallocated.
If this is NULL, a new block is allocated and a pointer to it is returned by the function.
size
New size for the memory block, in bytes.
If it is 0 and ptr points to an existing block of memory, the memory block pointed by ptr is deallocated and a NULL pointer is returned.

Return Value

 
    A pointer to the reallocated memory block, which may be either the same as the ptr argument or a new location.
    The type of this pointer is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
    If the function failed to allocate the requested block of memory, a NULL pointer is returned, and the memory block pointed to by argument ptr is left unchanged.


Example

 

/* realloc example: rememb-o-matic */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int input,n;
int count=0;
int * numbers = NULL;
int * more_numbers;

do {
printf ("Enter an integer value (0 to end): ");
scanf ("%d", &input);
count++;

more_numbers = (int*) realloc (numbers, count * sizeof(int));

if (more_numbers!=NULL) {
numbers=more_numbers;
numbers[count-1]=input;
}
else {
free (numbers);
puts ("Error (re)allocating memory");
exit (1);
}
} while (input!=0);

printf ("Numbers entered: ");
for (n=0;n<count;n++) printf ("%d ",numbers[n]);
free (numbers);

return 0;
}



   
The program prompts the user for numbers until a zero character is entered. Each time a new value is introduced the memory block pointed by numbers is increased by the size of an int.
 
上面是网上的对realloc的解释,但是我发现一个问题,如果对于ptr指针,如果第一次申请一块内存,然后与多个指针指向这个内存,然后有扩充内存后,遇到了这片内存不够扩充,然后换了一片内存,那么,仅仅是ptr指针指向了新的内存,而其他的仍然指向旧的内存,但是旧的内存已经销毁了,怎么解释呢?
 
自己才开始深入学习c/c++,所以如果谁知道为什么的话可以告诉我?然后我会补充在这篇日志上。

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C语言基础04--常量与变量发布时间:2022-07-14
下一篇:
【c】inline,__inline,__forceinline的详细说明发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap