何时在C中使用Calloc或Malloc

发布于 2025-02-08 02:14:42 字数 592 浏览 3 评论 0原文

什么更好/更高效 - callocmalloc

我想初始化结构,该结构是指同一结构的其他实例也是

变体1

person *new_person() {
    struct _person *person = calloc(1, sizeof(person));
    person->name = NULL;
    person->child = NULL; 
    return person;
}

变体2

person *new_person() {
    struct _person *person = malloc(sizeof(person));
    person->name = NULL;
    person->child = NULL;
    return person;
}

结构

typedef struct _person {
    *void name;
    struct _person *child;
} person;

What's better/more efficient - calloc or malloc?

I want to initialise structs, that refer to other instances of the same struct also

VARIANT 1

person *new_person() {
    struct _person *person = calloc(1, sizeof(person));
    person->name = NULL;
    person->child = NULL; 
    return person;
}

VARIANT 2

person *new_person() {
    struct _person *person = malloc(sizeof(person));
    person->name = NULL;
    person->child = NULL;
    return person;
}

The struct

typedef struct _person {
    *void name;
    struct _person *child;
} person;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2025-02-15 02:14:42

示例中有明显且更细微的问题:

  • *void name;是语法错误。

  • struct _person* person = calloc(1,sizeof(person)); nor struct _person* person = malloc(sizeof(person));将分配正确的内存量是因为sizeof(person)将评估指示中的指针在定义中,而不是定义为定义为一个的类型person struct _person

    typedef

    这是名称阴影的一个病理示例,因此,本地定义隐藏了外部范围中同一标识符的另一个定义。使用-wshadow让编译器检测并报告此类问题。

在这两个示例中,您都应用指针指向的数据的大小:

    struct _person *person = calloc(1, sizeof(*person));

关于是否使用calloc() malloc(),始终使用要安全得多calloc()由于以下原因:

  • 元素的大小和元素的数量是分开的,因此避免了整体大小计算中的愚蠢错误。
  • 将内存初始化为所有位零位,这是现代系统上所有标量类型的零值。这可能使您可以通过省略整数成员的明确初始化来简化代码,并将初始化迫使所有额外成员的初始化为0以后添加到结构定义中,该定义可能会缺少初始化语句。
  • 对于大数组,calloc()实际上比malloc(size) + memset(s,0,size) https://stackoverflow.com/a/18251590/4593267

答案:

malloc和calloc之间的差异?

There are obvious and more subtile problems in your examples:

  • *void name; is a syntax error.

  • Neither struct _person* person = calloc(1, sizeof(person)); nor struct _person* person = malloc(sizeof(person)); will allocate the correct amount of memory because sizeof(person) will evaluate to the size of the pointer person in the definition, not the type person defined as a typedef for struct _person.

    This is a pathological example of name shadowing, whereby a local definition hides another definition for the same identifier in an outer scope. Use -Wshadow to let the compiler detect and report this kind of problem.

In both examples, you should the size of the data pointed to by the pointer:

    struct _person *person = calloc(1, sizeof(*person));

Regarding whether to use calloc() or malloc(), it is much safer to always use calloc() for these reasons:

  • the size of the element and the number of elements are separate, thus avoiding silly mistakes in the overall size computation.
  • the memory is initialized to all bits zero, which is the zero value of all scalar types on modern systems. This may allow you to simplify the code by omitting explicit initialization of integer members and will force initialization to 0 of all extra members later added to the structure definition for which the allocation functions might be missing initializing statements.
  • for large arrays, calloc() is actually faster than malloc(size) + memset(s, 0, size), as is documented this answer: https://stackoverflow.com/a/18251590/4593267

A more general discussion of this topic is this other question:

Difference between malloc and calloc?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文