zoukankan      html  css  js  c++  java
  • 随便写写

    最后,内存分配直接用realloc来实现,该函数签名如下:
    void *realloc( void *memblock, size_t size );
    对于释放内存则直接利用memmove函数来实现
    Moves one buffer to another.
    void *memmove( void *dest, const void *src, size_t count );
    下段摘自MSDN
    Return Value
    Moves one buffer to another.
    void *memmove( void *dest, const void *src, size_t count );
    Return Value
    memmove returns the value of dest.
    Parameters
    dest
    Destination object
    src
    Source object
    count
    Number of bytes of characters to copy
    Remarks
    The memmove function copies count bytes of characters from src to dest. If some regions of the source area and the destination overlap, memmove ensures that the original source bytes in the overlapping region are copied before being overwritten.
     

    realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

    Parameters

    memblock

    Pointer to previously allocated memory block

    size

    New size in bytes

    Remarks

    The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc.

    The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.

    realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call

    _set_new_mode(1)
    

    early in your program, or link with NEWMODE.OBJ.

    When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support.

    有一点要注意,对于memblock为NULL的情况,该函数的作用与malloc一样,下面为sample:
      1:      long *pBuffer;
       2:      long size;
       3:      pBuffer = (long *)malloc(2*sizeof(long));
       4:      *pBuffer = 1;
       5:      *(pBuffer+1)=2;
       6:   
       7:      long l1 = *(pBuffer+1);
       8:      size = _msize(pBuffer);
       9:      cout << "size:" << size << "\t" << (*pBuffer) << "\t" << l1 << endl;
      10:   
      11:      //realloc
      12:      realloc(pBuffer,3*sizeof(long));
      13:      *(pBuffer+2)=6;
      14:      l1 = *(pBuffer+1);
      15:      long l2 = *(pBuffer+2);
      16:      size = _msize(pBuffer);
      17:      cout << "size:" << size << "\t" << (*pBuffer) << "\t" << l1 << "\t" << l2 << endl;
      18:      memmove(pBuffer+1,pBuffer+2,sizeof(long));
      19:      
      20:      size = _msize(pBuffer);
      21:      l1 = *(pBuffer+1);
      22:      //l2 = *(pBuffer+2);
      23:      cout << "size:" << size << "\t" << (*pBuffer) <<  "\t" << l1 << "\t" << l2 << endl;
    Author:repository
    From:  http://repository.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    CtsVerifier-Bluetooth-LE-SEcure-ClientServer-Test测试pass但是无法选择PassButton
    error: 'LOG_TAG' macro redefined
    AndroidBuildSystem资料
    [dumpsys input]dumpsys input没有KeyEvent-KeyCode-MotionEvent键值
    [GTS]GtsSecurityHostTestCases#testNoExemptionsForSocketsBetweenCoreAndVendorBan
    [Gts]Fail-GtsTvTestCases#testLauncherChange
    [Cts-Verifier]waiver-Camera-ITS-Test
    历史角度看Support Library 到 AndroidX
    基于大疆无人机SDK二次开发
    关于Support库28及以上版本无法查看源码问题
  • 原文地址:https://www.cnblogs.com/repository/p/2247258.html
Copyright © 2011-2022 走看看