为什么这本书说我必须铸造malloc?

时间:2022-09-11 09:30:52

Today I reached page 167 of The C Programming Language (second edition Brian W. Kernighan & Dennis M. Ritchie) and found that the author says I must cast malloc. Here is the part from the book:

今天我读了C语言的第167页(第二版Brian W. Kernighan & Dennis M. Ritchie),发现作者说我必须出演malloc。以下是书中的部分:

7.8.5 Storage Management

7.8.5存储管理

The functions malloc and calloc obtain blocks of memory dynamically.

malloc和calloc函数动态获取内存块。

void *malloc(size_t n)

returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied.

返回一个指向未初始化存储的n字节的指针,如果请求不能满足,则返回NULL。

void *calloc(size_t n, size_t size)

returns a pointer to enough free space for an array of n objects of the specified size, or NULL if the request cannot be satisfied. The storage is initialized to zero. The pointer returned by malloc or calloc has the proper alignment for the object in question, but it must be cast into the appropriate type, as in

返回一个指向指定大小的n个对象数组的足够*空间的指针,如果请求不能满足,则返回NULL。存储被初始化为零。malloc或calloc返回的指针对该对象具有适当的对齐方式,但必须将其转换为适当的类型,如in

int *ip;
ip = (int *) calloc(n, sizeof(int));

I already know that malloc (and its family) returns type void*, and there are good explanations why not to cast malloc.

我已经知道malloc(及其家族)返回类型void*,并且有很好的解释为什么不使用malloc。

But my question is: Why does the book say I should cast it?

但我的问题是:为什么这本书说我应该投它?

1 个解决方案

#1


202  

From http://computer-programming-forum.com/47-c-language/a9c4a586c7dcd3fe.htm:

从http://computer-programming-forum.com/47-c-language/a9c4a586c7dcd3fe.htm:

In pre-ANSI C -- as described in K&R-1 -- malloc() returned a char * and it was necessary to cast its return value in all cases where the receiving variable was not also a char *. The new void * type in Standard C makes these contortions unnecessary.

在pre-ANSI C中——正如K&R-1中所描述的——malloc()返回了一个char *,在所有接收变量不是char *的情况下,必须对其返回值进行转换。标准C中的新的void *类型使这些扭曲变得不必要。

To save anybody from the embarrassment of leaping needlessly to the defence of K&R-2, I asked Dennis Ritchie for an opinion that I could quote on the validity of the sentence cited above from page 142. He replied:

为了避免任何人不必要地跳起来为K&R-2辩护而感到尴尬,我向丹尼斯·里奇征求了一个意见,我可以引用上面142页引用的句子的有效性。他回答说:

In any case, now that I reread the stuff on p. 142, I think it's wrong; it's written in such a way that it's not just defensive against earlier rules, it misrepresents the ANSI rules.

无论如何,现在我重读了142页,我认为这是错的;它的书写方式不仅是对早期规则的防御,它还歪曲了ANSI规则。

#1


202  

From http://computer-programming-forum.com/47-c-language/a9c4a586c7dcd3fe.htm:

从http://computer-programming-forum.com/47-c-language/a9c4a586c7dcd3fe.htm:

In pre-ANSI C -- as described in K&R-1 -- malloc() returned a char * and it was necessary to cast its return value in all cases where the receiving variable was not also a char *. The new void * type in Standard C makes these contortions unnecessary.

在pre-ANSI C中——正如K&R-1中所描述的——malloc()返回了一个char *,在所有接收变量不是char *的情况下,必须对其返回值进行转换。标准C中的新的void *类型使这些扭曲变得不必要。

To save anybody from the embarrassment of leaping needlessly to the defence of K&R-2, I asked Dennis Ritchie for an opinion that I could quote on the validity of the sentence cited above from page 142. He replied:

为了避免任何人不必要地跳起来为K&R-2辩护而感到尴尬,我向丹尼斯·里奇征求了一个意见,我可以引用上面142页引用的句子的有效性。他回答说:

In any case, now that I reread the stuff on p. 142, I think it's wrong; it's written in such a way that it's not just defensive against earlier rules, it misrepresents the ANSI rules.

无论如何,现在我重读了142页,我认为这是错的;它的书写方式不仅是对早期规则的防御,它还歪曲了ANSI规则。