“Use of realloc正确的方法”

huangapple go评论61阅读模式
英文:

Use of realloc correct approach

问题

I have the following line of code, which snippet correctly uses realloc?

我有以下行代码,哪个片段正确使用了realloc?

I have a suspicioun that the realloc call in *delete_choices line is incorrectly used is this correct?

我怀疑*delete_choices行中的realloc调用是否被错误使用了,这是正确的吗?

delete_choices = (char)malloc(files_max);

delete_choices = (char)malloc(files_max);

if (files_size >= 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);
}
}

Is this the correct usage?

这是正确的用法吗?

delete_choices = (char)realloc(*delete_choices, files_max);

delete_choices = (char)realloc(*delete_choices, files_max);

Really confused about this

对此感到非常困惑

英文:

I have the following line of code, which snippet correctly uses realloc?

I have a suspicioun that the realloc call in *delete_choices line is incorrectly used is this correct?

  *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);
    }

Is this the correct usage?

*delete_choices = (char*)realloc(*delete_choices, files_max);

Really confused about this

答案1

得分: 3

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);
}


正确的表达是:
```c
*delete_choices = realloc(*delete_choices, files_max);

此外,上述代码片段存在潜在的未定义行为风险,因为如果realloc()失败并返回NULL指针,您将失去对通过malloc()分配的原始内存的访问权(假设malloc()成功),从而导致内存泄漏。

解决方案:

使用临时指针来存储realloc()的返回值:

/* 是的,用法是正确的。 */
char *const tmp = realloc(*delete_choices, files_max);
if (!tmp) {
    perror("realloc()");
    complain();
}
      
*delete_choices = tmp;

注意:不要对malloc()及其相关函数的返回值进行强制类型转换。这些函数返回一个通用的void *,会自动转换为任何其他指针类型。强制类型转换是多余的,只会使代码变得混乱。

英文:
*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);
}

The correct expression is:

*delete_choices = realloc (*delete_choices, files_max);

Furthermore, the above code snippet risks invoking undefined behaviour because if realloc() fails and returns a NULL pointer, you'd lose access to the original memory allocated through malloc() (assuming malloc() had succeeded), and leak memory.

Solution:

Use a temporary pointer to store the return value of realloc():

/* Yes, the usage is correct. */
char *const tmp = realloc(*delete_choices, files_max);
if (!tmp) {
    perror("realloc()");
    complain();
}
      
*delete_choices = tmp;

Note: Do not cast the return of malloc() and family. These functions return a generic void * that is implicitly converted to any other pointer type. The cast is redundant and only serves to clutter one's code.

答案2

得分: 1

*delete_choices = (char*)realloc( *delete_choices, files_max ); 是正确的用法。

你传递的类型必须等于返回的类型。

在你的情况下,delete_choices 似乎是类型为 char**,因此 *delete_choices 是类型为 char*,所以这两种类型不相等。

一般情况下:t = (T)realloc( t, n );

英文:

*delete_choices = (char*)realloc( *delete_choices, files_max ); is the correct usage.

The type that you pass must be equal to the type returned.

In your case, delete_choices appears to be of type char**, therefore *delete_choices is of type char*, so these two types are not equal.

In general: t = (T)realloc( t, n );

huangapple
  • 本文由 发表于 2023年5月24日 18:24:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定