gpt4 book ai didi

c - malloc 和 free 的实现

转载 作者:行者123 更新时间:2023-12-01 14:00:53 24 4
gpt4 key购买 nike

我对 malloc 后跟 free 有疑问。

char *p = (char *) malloc(100);
p++;
free(p);

在上面的场景中,free(p) 会释放多少字节的内存?是 99 还是 100?

最佳答案

这是未定义的行为。 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 - malloc 和 free 的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23919637/

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