This question already has an answer here:
这个问题已经有了答案:
- Do I cast the result of malloc? 27 answers
- 我是否使用了malloc的结果?27日答案
I wanted to ask about the following case:
我想问一下以下情况:
char *temp;
temp = malloc(10);
Since the return type of malloc
is void*
, will the pointer returned by the malloc
be implicitly cast to char*
type before being assigned to temp? What does the standard say in this regard?
由于malloc的返回类型是void*, malloc返回的指针会在被分配到temp之前隐式地转换为char*类型吗?在这方面标准是怎么说的?
If our pointer variable is some struct type for example:
如果我们的指针变量是某种结构类型,例如:
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
If we allocate memory to temp without casting it to struct node*
type, will it be implicitly cast to struct node*
type or is it necessary to explicitly cast it to struct node*
type?
如果我们将内存分配给temp而不将其转换为struct node* type,那么它将被隐式转换为struct node* type,还是有必要显式地将其转换为struct node* type?
4 个解决方案
#1
33
A void pointer in C can be assigned to any pointer without an explicit cast.
C中的空指针可以分配给任何没有显式转换的指针。
#2
51
If you like the "don't repeat yourself" mindset, it should be appealing that you don't need to repeat the type name from the declaration of the variable, in the malloc()
call. Because, as folks have pointed out, you don't: pointers convert to and from void *
without loss, with the exception of function pointers.
如果您喜欢“不要重复自己”的思想,那么在malloc()调用中,不需要重复变量声明中的类型名,这应该很有吸引力。因为,正如人们指出的那样,您不需要:指针转换为或转换为void *而不会丢失,函数指针除外。
Also, on that note, you don't need to repeat yourself with the use of sizeof
either. Your second example, when allocating a structure, can be written like this:
同样,你也不需要重复使用sizeof。第二个例子,在分配结构时,可以这样写:
struct node *temp;
temp = malloc(sizeof *temp);
Which in my not so humble opinion is the best way.
在我看来,这是最好的办法。
Avoiding repeating yourself cuts down on the number of things you write, which in turn cuts down on the risk that any of those things are wrong.
避免重复你自己会减少你写的东西的数量,这反过来也会减少你写错东西的风险。
Note the asterisk in the sizeof
argument, this means "the size of the object pointed to by this pointer", which is of course the same as "the size of the type struct node
" but without repeating the type name. This is because sizeof
computes (at compile-time!) the size of the expression that is its argument. For this case. Just like sizeof 3
computes the size of an expression of type int
, sizeof *temp
computes the size of an instance of struct node
.
注意sizeof参数中的星号,这意味着“这个指针指向的对象的大小”,当然它与“类型struct节点的大小”相同,但是不重复类型名称。这是因为sizeof计算(在编译时!)它的参数表达式的大小。对于这种情况。就像sizeof 3计算int类型表达式的大小一样,sizeof *temp计算结构节点实例的大小。
Sure, you do repeat something, i.e. the variable name itself, but that is often a simpler expression and easier to get right, and also can be easier for the compiler to spot an error in.
当然,您确实需要重复一些东西,比如变量名本身,但是这通常是一个更简单的表达式,更容易得到正确的结果,而且编译器也更容易发现错误。
#3
10
C implicitly casts from and to void*
, so the cast will be done automatically. In C++ only conversion to void*
would be done implicitly, for the other direction an explicit cast is required.
C隐式地从和到void*进行强制类型转换,因此强制类型转换将自动完成。在c++中,只有转换到void*是隐式的,对于另一个方向,需要显式的转换。
#4
3
In C++ you must explicitly cast, but this is really just the language telling you off for doing it.
In c there isn't really any need to cast, memory is just memory - I will have to do a search to see if the latest C standard requires it.
在c++中,您必须显式地强制转换,但这实际上只是一种语言,告诉您不要这样做。在c中,并不需要强制转换,内存只是内存——我需要进行搜索,看看最新的c标准是否需要它。
#1
33
A void pointer in C can be assigned to any pointer without an explicit cast.
C中的空指针可以分配给任何没有显式转换的指针。
#2
51
If you like the "don't repeat yourself" mindset, it should be appealing that you don't need to repeat the type name from the declaration of the variable, in the malloc()
call. Because, as folks have pointed out, you don't: pointers convert to and from void *
without loss, with the exception of function pointers.
如果您喜欢“不要重复自己”的思想,那么在malloc()调用中,不需要重复变量声明中的类型名,这应该很有吸引力。因为,正如人们指出的那样,您不需要:指针转换为或转换为void *而不会丢失,函数指针除外。
Also, on that note, you don't need to repeat yourself with the use of sizeof
either. Your second example, when allocating a structure, can be written like this:
同样,你也不需要重复使用sizeof。第二个例子,在分配结构时,可以这样写:
struct node *temp;
temp = malloc(sizeof *temp);
Which in my not so humble opinion is the best way.
在我看来,这是最好的办法。
Avoiding repeating yourself cuts down on the number of things you write, which in turn cuts down on the risk that any of those things are wrong.
避免重复你自己会减少你写的东西的数量,这反过来也会减少你写错东西的风险。
Note the asterisk in the sizeof
argument, this means "the size of the object pointed to by this pointer", which is of course the same as "the size of the type struct node
" but without repeating the type name. This is because sizeof
computes (at compile-time!) the size of the expression that is its argument. For this case. Just like sizeof 3
computes the size of an expression of type int
, sizeof *temp
computes the size of an instance of struct node
.
注意sizeof参数中的星号,这意味着“这个指针指向的对象的大小”,当然它与“类型struct节点的大小”相同,但是不重复类型名称。这是因为sizeof计算(在编译时!)它的参数表达式的大小。对于这种情况。就像sizeof 3计算int类型表达式的大小一样,sizeof *temp计算结构节点实例的大小。
Sure, you do repeat something, i.e. the variable name itself, but that is often a simpler expression and easier to get right, and also can be easier for the compiler to spot an error in.
当然,您确实需要重复一些东西,比如变量名本身,但是这通常是一个更简单的表达式,更容易得到正确的结果,而且编译器也更容易发现错误。
#3
10
C implicitly casts from and to void*
, so the cast will be done automatically. In C++ only conversion to void*
would be done implicitly, for the other direction an explicit cast is required.
C隐式地从和到void*进行强制类型转换,因此强制类型转换将自动完成。在c++中,只有转换到void*是隐式的,对于另一个方向,需要显式的转换。
#4
3
In C++ you must explicitly cast, but this is really just the language telling you off for doing it.
In c there isn't really any need to cast, memory is just memory - I will have to do a search to see if the latest C standard requires it.
在c++中,您必须显式地强制转换,但这实际上只是一种语言,告诉您不要这样做。在c中,并不需要强制转换,内存只是内存——我需要进行搜索,看看最新的c标准是否需要它。