zoukankan      html  css  js  c++  java
  • free 释放内存

    http://www.cplusplus.com/reference/cstdlib/free/

    free

    void free (void* ptr);
    Deallocate memory block

    A block of memory previously allocated by a call to malloccalloc or realloc is deallocated, making it available again for further allocations.

    If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior.

    If ptr is a null pointer, the function does nothing.

    Notice that this function does not change the value of ptr itself, hence it still points to the same (now invalid) location.

    Parameters

    ptr
    Pointer to a memory block previously allocated with malloccalloc or realloc.

    Return Value

    none

    Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    /* free example */
    #include <stdlib.h>     /* malloc, calloc, realloc, free */
    
    int main ()
    {
      int * buffer1, * buffer2, * buffer3;
      buffer1 = (int*) malloc (100*sizeof(int));
      buffer2 = (int*) calloc (100,sizeof(int));
      buffer3 = (int*) realloc (buffer2,500*sizeof(int));
      free (buffer1);
      free (buffer3);
      return 0;
    }



    This program has no output. It just demonstrates some ways to allocate and free dynamic memory using the C stdlibfunctions.

    Data races

    Only the storage referenced by ptr is modified. No other storage locations are accessed by the call.
    If the function releases a unit of storage that is reused by a call to allocation functions (such as calloc or malloc), the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.

    Exceptions (C++)

    No-throw guarantee: this function never throws exceptions.

    If ptr does not point to a memory block previously allocated with malloccalloc or realloc, and is not a null pointer, it causes undefined behavior.

    See also

  • 相关阅读:
    [转]子网掩码与子网划分
    子网划分的两个例子
    浅谈算法和数据结构系列汇总(转)
    拓扑排序和关键路径
    生成树及最小生成树
    图的遍历
    单选按钮的显示与隐藏列项
    CSS3实现鼠标移动到图片上图片变大
    优酷的视频地址获取接口
    提交按钮ajax方式
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10344430.html
Copyright © 2011-2022 走看看