zoukankan      html  css  js  c++  java
  • 学习c/c++之 realloc (仅供参考)

     
    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++,所以如果谁知道为什么的话可以告诉我?然后我会补充在这篇日志上。
  • 相关阅读:
    html 知识
    mysql use mysql hang
    微信机器人 简化版
    Tk::Table
    好友消息和群消息区别
    完整的微信登陆 接收消息流程
    Python OOP知识积累
    Python OOP知识积累
    JSTL EL 详解
    JSP中的EL表达式详细介绍
  • 原文地址:https://www.cnblogs.com/tiankonguse/p/realloc.html
Copyright © 2011-2022 走看看