C库函数_malloc calloc & realloc

本文详细介绍了C语言中三种重要的内存管理函数:malloc、calloc及realloc的功能、参数与使用方法,并解释了它们之间的区别。

一. malloc

函数原型: 

void* malloc( size_t size ); //Defined in header <stdlib.h>  返回任意类型值地址,可以强转

Parameters:

size -- number of bytes to allocate

      Return value:

Pointer to the beginning of newly allocated memory or null pointer if error has occurred. 

The pointer must be deallocated with free().

  注解:

1. if allocation succeeds, returns a pointer to the lowest (first) byte in the allocated memory block that is suitably aligned for any object type

2. malloc is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage

3. If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be 

returned that may not be used to access storage)

二. calloc

函数原型:

void* calloc( size_t num, size_t size );

Parameters:

num -- number of objects

size -- size of each objects

Return value:

与 malloc 一样

注解: 

1. Allocates memory for an array of num objects of size size and initializes all bits in the allocated storage to zero

     2. If allocation succeeds, returns a pointer to the lowest (first) byte in the allocated memory block that is suitably aligned for any object type.

     3. If size is zero, the behavior is implementation defined (null pointer may be returned, or some non-null pointer may be returned that may not be                                  used to access storage)


三. realloc

函数原型:

void *realloc( void *ptr, size_t new_size );

Parameters:

       ptr -- pointer to the memory area to be allocated
   new_size -- new size of  the array

Return value : 

   同上


注解:

1. Reallocates the given area of memory. It must be previously allocated by malloc()calloc() or realloc() and not yet freed with free,                                     otherwise, the results are undefined.

      2. f there is not enough memory, the old memory block is not freed and null-pointer is returned

      3. If ptr is NULL, the behavior is the same as calling malloc(new_size).

      





### `__real_malloc`, `__real_realloc`, `__real_calloc` 的作用与使用场景 在某些高级内存管理或调试场景中,C 程序会通过 GNU 链接器的 `--wrap` 机制对标准库函数如 `malloc`、`realloc` `calloc` 进行包装,以实现自定义的日志记录、性能分析、内存泄漏检测等功能。在此背景下,`__real_malloc`、`__real_realloc` `__real_calloc` 成为访问原始标准库函数的关键接口。 --- #### `__real_malloc` `__real_malloc` 是对系统标准 `malloc` 函数的真实引用,在链接时通过 `--wrap malloc` 被重定向到 `__wrap_malloc` 后,程序中直接调用的 `malloc` 实际上是被替换后的封装函数;而 `__real_malloc` 则用于绕过包装,调用真正的底层 `malloc` 实现[^1]。 其函数原型如下: ```c void* __real_malloc(size_t size); ``` **典型使用场景:** - 在自定义的 `__wrap_malloc` 函数内部调用原始 `malloc` 来完成实际内存分配。 - 用于需要确保不经过任何中间层直接调用原生内存分配器的场合。 示例代码: ```c void* __wrap_malloc(size_t size) { printf(&quot;Intercepted malloc of %zu bytes\n&quot;, size); return __real_malloc(size); // 调用真实的 malloc } ``` --- #### `__real_realloc` `__real_realloc` 对应于标准库中的 `realloc` 函数,用于调整已分配内存块的大小。它通常在 `__wrap_realloc` 中被调用以完成实际的内存扩展或收缩操作[^3]。 函数原型如下: ```c void* __real_realloc(void* ptr, size_t size); ``` 该函数的行为包括: - 如果 `ptr` 为 `NULL`,则等价于 `malloc(size)`。 - 如果当前内存块后有足够空间,则直接扩展。 - 否则分配新内存并复制旧数据,然后释放原内存。 **典型使用场景:** - 动态数组扩容(如构建字符串缓冲区)。 - 内存调试工具中追踪内存重分配行为。 示例代码: ```c void* __wrap_realloc(void* ptr, size_t size) { printf(&quot;Reallocating %p to %zu bytes\n&quot;, ptr, size); return __real_realloc(ptr, size); // 调用真正的 realloc } ``` --- #### `__real_calloc` 虽然用户未直接提问 `__real_calloc`,但作为标准内存分配函数族的一部分,它同样适用于类似的包装调试机制。`__real_calloc` 用于分配并清零一块指定数量大小的内存区域,其函数原型如下: ```c void* __real_calloc(size_t nmemb, size_t size); ``` 与 `__real_malloc` 不同的是,`calloc` 分配的内存会被初始化为零,适用于需要初始化的数据结构如哈希表、链表节点等。 **典型使用场景:** - 构建自动清零的数据结构。 - 在内存监控工具中替代 `calloc` 并记录分配信息。 --- ### 使用注意事项 - 在使用 `__real_realloc` 时需注意返回值可能不同于输入指针,因此必须将结果赋值回原指针变量。 - 所有动态分配的内存都应通过 `free` 或对应的 `__real_free` 正确释放,否则可能导致内存泄漏[^4]。 - 包装函数应保持与原函数签名一致,并确保正确调用 `__real_*` 版本。 --- ### 示例:完整包装 `malloc` `realloc` ```c #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void* __wrap_malloc(size_t size) { printf(&quot;[Malloc] Allocating %zu bytes\n&quot;, size); return __real_malloc(size); } void* __wrap_realloc(void* ptr, size_t size) { printf(&quot;[Realloc] Reallocating %p to %zu bytes\n&quot;, ptr, size); return __real_realloc(ptr, size); } int main() { void* mem = malloc(16); // 实际调用 __wrap_malloc mem = realloc(mem, 32); // 实际调用 __wrap_realloc free(mem); return 0; } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值