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
Return ValueExample
/* realloc example: rememb-o-matic */ #include
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++,所以如果谁知道为什么的话可以告诉我?然后我会补充在这篇日志上。 |