博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
学习c/c++之 realloc (仅供参考)
阅读量:7095 次
发布时间:2019-06-28

本文共 2755 字,大约阅读时间需要 9 分钟。

 
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 
 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 
#include
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

   
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++,所以如果谁知道为什么的话可以告诉我?然后我会补充在这篇日志上。

转载于:https://www.cnblogs.com/tiankonguse/archive/2011/11/19/realloc.html

你可能感兴趣的文章
[POJ3169] Layout
查看>>
JAVA虚拟机:对象的创建过程
查看>>
form表单post请求保护 隐藏秘钥
查看>>
作用域插槽,用来属性插入
查看>>
201521123068《Java程序设计》第1周学习总结
查看>>
在服务器上安装MongoDB
查看>>
20140104
查看>>
规则(1)
查看>>
CF1025B Weakened Common Divisor【数论/GCD/思维】
查看>>
【Java】使用CSVUtils生成文件并供下载
查看>>
用户态和内核态
查看>>
VR+生物plus 遐想
查看>>
Java并发编程:线程控制
查看>>
今天聊一聊Java引用类型的强制类型转换
查看>>
把数据保存到数据库archives表时出错,请检查
查看>>
JavaSE基本语法、数据类型、操作符等:int、long、Integer、Long、if、else、for、while...
查看>>
解析XML文件
查看>>
牛客练习赛46
查看>>
netty线程模型
查看>>
Codeforces Round #237 Div.2 A
查看>>