gpt4 book ai didi

c - 释放 C 中分配的内存

转载 作者:行者123 更新时间:2023-11-30 20:12:52 25 4
gpt4 key购买 nike

假设我已经分配了足够大的char**数组。

为什么我可以free(arr)但不是free(arr + 5)

请注意,我说的不是 arr[0]arr[5] .

最佳答案

您始终使用 malloc()realloc()calloc( 返回的指针调用 free() )。您不得传递任何其他指针。

来自free()手册:

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc(), or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

关于c - 释放 C 中分配的内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33973265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com