c++ - 实际获得的动态分配内存的大小怎么确定
怪我咯
怪我咯 2017-04-17 14:20:17
0
5
657
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(5)
伊谢尔伦

I have seen it on Zhihu:

Let me ask you a question first: there is no information about the size of the memory block in the pointer, so how can free know how big the memory block to be released is? Therefore, for most memory allocators, the actual memory requested by malloc is several bytes larger than the space you require, and additional data is stored in it to record how big the memory is, which is usually stored directly to the left of the pointer. When free, the memory block after the pointer address minus a constant is read to obtain the memory block information. Therefore, if you free a pointer that does not point to the beginning of the memory block, other data will be incorrectly interpreted as memory block information during free, which (high probability) will cause the program to crash. Of course, modern memory allocators will adopt different allocation strategies for memory applications of different sizes, but no matter what the strategy is, it is a very dangerous move to free a pointer that is not from malloc.

See: https://www.zhihu.com/questio...

黄舟

How much more memory is actually allocated than requested is not specified in the standard and depends on the implementation of malloc. This is an implementation that developers do not need to care about.

小葫芦

Determine the actual allocated memory size

There is no one-size-fits-all solution. Because this is an implementation detail of the memory allocator. If you have to know, you have to look at how the memory allocator you use allocates memory.

大家讲道理

This part is mentioned in CSAPP. Not only will there be an extra record size header, but also more memory than requested will be obtained due to the alignment operation

左手右手慢动作

The operating system needs to know how much memory is allocated for each pointer. When we called delete, we did not tell the operating system how much memory the area occupied by the pointer occupied, so there must be a place where this value is stored, and This value has different implementations in different operating systems. For example, the simplest implementation is to use a byte in front of each pointer to save the length of the memory allocation. Generally, the operating system will provide a function to obtain the actual memory size. , such as functions like malloc_size.
If you are interested, you can take a look at the redis source code, zmalloc.h and zmalloc.c
https://github.com/antirez/re...

#if defined(USE_TCMALLOC)
#define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR))
#include <google/tcmalloc.h>
#if (TC_VERSION_MAJOR == 1 && TC_VERSION_MINOR >= 6) || (TC_VERSION_MAJOR > 1)
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) tc_malloc_size(p)
#else
#error "Newer version of tcmalloc required"
#endif

#elif defined(USE_JEMALLOC)
#define ZMALLOC_LIB ("jemalloc-" __xstr(JEMALLOC_VERSION_MAJOR) "." __xstr(JEMALLOC_VERSION_MINOR) "." __xstr(JEMALLOC_VERSION_BUGFIX))
#include <jemalloc/jemalloc.h>
#if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2)
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) je_malloc_usable_size(p)
#else
#error "Newer version of jemalloc required"
#endif

#elif defined(__APPLE__)
#include <malloc/malloc.h>
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) malloc_size(p)
#endif

You can also learn about the implementation of tcmalloc and jemalloc

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template