提问人:HyperCoderSuperion 提问时间:5/24/2023 最后编辑:Mike NakisHyperCoderSuperion 更新时间:5/24/2023 访问量:81
使用 realloc 正确方法 [重复]
Use of realloc correct approach [duplicate]
问:
我有以下代码行,哪个代码段正确使用 realloc?
我怀疑 *delete_choices 行中的 realloc 调用使用不正确,这是正确的吗?
*delete_choices = (char*)malloc(files_max);
if (files_size >= files_max)
{
files_max *= 2;
files = (struct dirent*)realloc(files, sizeof(struct dirent) * files_max);
*delete_choices = (char*)realloc(delete_choices, files_max);
}
这是正确的用法吗?
*delete_choices = (char*)realloc(*delete_choices, files_max);
对此真的很困惑
答:
1赞
Mike Nakis
5/24/2023
#1
*delete_choices = (char*)realloc( *delete_choices, files_max );
是正确的用法。
传递的类型必须等于返回的类型。
在你的例子中,似乎是类型,因此是类型,所以这两种类型不相等。delete_choices
char**
*delete_choices
char*
通常:t = (T)realloc( t, n );
3赞
Harith
5/24/2023
#2
*delete_choices = (char*)malloc(files_max);
if (files_size >= files_max)
{
files_max *= 2;
files = (struct dirent*)realloc(files, sizeof (struct dirent) * files_max);
*delete_choices = (char*)realloc(delete_choices, files_max);
}
正确的表达式是:
*delete_choices = realloc (*delete_choices, files_max);
此外,上述代码片段可能会调用未定义的行为,因为如果失败并返回指针,您将无法访问通过(假设已成功)分配的原始内存,并泄漏内存。realloc()
NULL
malloc()
malloc()
溶液:
使用临时指针来存储 的返回值:realloc()
/* Yes, the usage is correct. */
char *const tmp = realloc(*delete_choices, files_max);
if (!tmp) {
perror("realloc()");
complain();
}
*delete_choices = tmp;
注意:不要投回和家人的回归。这些函数返回一个泛型,该泛型隐式转换为任何其他指针类型。强制转换是多余的,只会使代码混乱。malloc()
void *
评论
1赞
Eric Postpischil
5/24/2023
首先,也是最重要的一点是,在两个地方都使用正确的地址表达式进行重新分配,而不是在一个地方和另一个地方。不使用强制转换不是首要的,因为不正确的表达式是实际的错误(非常重要),而强制转换是用于减少错误发生的代码模式(不太重要)。这个“答案”并没有明确回答所提出的问题。*delete_choices
delete_choices
*delete_choices
下一个:C 语言中的动态字符串数组
评论
realloc
NULL
sizeof char
malloc
realloc