随便写写

最后,内存分配直接用realloc来实现,该函数签名如下:
void realloc( void memblock, size_tsize);
对于释放内存则直接利用memmove函数来实现
Moves one buffer to another.
void memmove( void dest, const void src, size_tcount*);
下段摘自MSDN
Return Value
Moves one buffer to another.
void memmove( void dest, const void src, size_tcount*);
Return Value
memmove returns the value ofdest.
Parameters
dest
Destination object
src
Source object
count
Number of bytes of characters to copy
Remarks
The memmove function copiescountbytes of characters fromsrctodest.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.

realloccalls mallocin order to use the C++_set_new_modefunction 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, seeUsing 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;

原文链接: https://www.cnblogs.com/repository/archive/2011/11/13/2247258.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/36371

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午1:08
下一篇 2023年2月8日 下午1:09

相关推荐