C语言 malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15222774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:33:53  来源:igfitidea点击:

malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))

cmemoryallocationmalloc

提问by sabergeek

I acknowledge that all three of these have a different meaning. But, I don't understand on what particular instances would each of these apply. Can anyone share an example for each of these? Thank you.

我承认这三个都有不同的含义。但是,我不明白这些适用于哪些特定情况。任何人都可以为每一个分享一个例子吗?谢谢你。

       malloc(sizeof(int))
       malloc(sizeof(int *))
(int *)malloc(sizeof(int))

回答by Gort the Robot

malloc(sizeof(int))means you are allocating space off the heap to store an int. You are reserving as many bytes as an intrequires. This returns a value you should cast to int *. (A pointer to an int.)As some have noted, typical practice in C is to let implicit casting take care of this.

malloc(sizeof(int))意味着您正在从堆中分配空间来存储int. 您根据int需要保留尽可能多的字节。 这将返回一个您应该转换为的值int *。(指向 an 的指针int。)正如一些人所指出的,C 中的典型做法是让隐式转换来处理这个问题。

malloc(sizeof(int*))means you are allocating space off the heap to store a pointer to an int. You are reserving as many bytes as a pointer requires. This returns a value you should cast to an int **. (A pointer to a pointer to an int.)

malloc(sizeof(int*))意味着您正在从堆分配空间来存储指向int. 您正在保留指针所需的字节数。这将返回一个您应该转换为int **. (指向指向 . 的指针的指针int。)

(int *)malloc(sizeof(int))is exactly the same as the first call, but with the the result explicitly casted to a pointer to an int.

(int *)malloc(sizeof(int))与第一次调用完全相同,但结果显式转换为指向int.

Note that on many architectures, an intis the same size as a pointer, so these will seem (incorrectly) to be all the same thing. In other words, you can accidentally do the wrong thing and have the resulting code still work.

请注意,在许多体系结构上, anint与指针的大小相同,因此它们看起来(错误地)都是一样的。换句话说,您可能不小心做错了事,结果代码仍然可以工作。

回答by wildplasser

The syntax pattern that is most foolproof is:

最万无一失的语法模式是:

 int *p;
 p = malloc (cnt * sizeof *p);

This syntax will not force you to change the code if the type (and or size...) of *p changes, eg in

如果 *p 的类型(和或大小...)发生变化,则此语法不会强制您更改代码,例如在

 struct mysstruct *q;
 q = malloc (cnt * sizeof *q);

Which will avoid problems like

这将避免诸如

struct mysstruct *z;
z = malloc (cnt * sizeof(struct hisstruct)); // Auch!

, plus: the sizeof exprform is also shorter.

,加:sizeof expr表格也更短。



UPDATE: to demonstrate the correctness of p = malloc(CNT * sizeof *p)this test program:

更新:为了证明p = malloc(CNT * sizeof *p)这个测试程序的正确性:

#include <stdio.h>
#include <stdlib.h>

struct mystruct {
        int i;
        char s[14];
        };
int main(void)
{
struct mystruct *p;
size_t siz;

siz = sizeof p;
printf("Sizeof p := %zu\n", siz);

siz = sizeof *p;
printf("Sizeof *p := %zu\n", siz);

printf("Allocating %zu (%u structs, each of size %zu) bytes to be assigned to p...\n"
        , 10u * sizeof *p
        , 10u, sizeof *p
        );
p = malloc(10 * sizeof *p);

return 0;
}

Which outputs here:

这里的输出:

Sizeof p := 8
Sizeof *p := 20
Allocating 200 (10 structs, each of size 20) bytes to be assigned to p...