Understanding the Expression ((size_t *)ptr - 1) in C/C++
The expression '( (size_t *)ptr - 1)' casts the pointer 'ptr' to a 'size_t' pointer and then subtracts 1 from it.
This expression is commonly used when dealing with memory allocation functions like 'malloc' or 'calloc'. In these functions, the memory allocated is typically preceded by a header that stores information about the allocated block, such as its size. By subtracting 1 from the pointer, we can access this header information.
For example, if 'ptr' points to the beginning of a dynamically allocated block of memory, '( (size_t *)ptr - 1)' will give us a pointer to the header information just before the block.
原文地址: https://www.cveoy.top/t/topic/qkZk 著作权归作者所有。请勿转载和采集!