为什么 "The C Programming Language" 书说我必须转换 malloc?
Why does "The C Programming Language" book say I must cast malloc?
今天我翻到了 The C Programming Language (second edition Brian W. Kernighan & Dennis M. Ritchie) 的第 167 页,发现作者说我必须投 malloc
。这是书中的部分内容:
7.8.5 Storage Management
The functions malloc and calloc obtain blocks of memory dynamically.
void *malloc(size_t n)
returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied.
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
int *ip;
ip = (int *) calloc(n, sizeof(int));
我已经知道 malloc
(及其家族)returns 输入 void* 和 there are good explanations why not to cast malloc
.
但我的问题是:为什么书上说我应该施放它?
来自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.
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:
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.
今天我翻到了 The C Programming Language (second edition Brian W. Kernighan & Dennis M. Ritchie) 的第 167 页,发现作者说我必须投 malloc
。这是书中的部分内容:
7.8.5 Storage Management
The functions malloc and calloc obtain blocks of memory dynamically.
void *malloc(size_t n)
returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied.
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
int *ip; ip = (int *) calloc(n, sizeof(int));
我已经知道 malloc
(及其家族)returns 输入 void* 和 there are good explanations why not to cast malloc
.
但我的问题是:为什么书上说我应该施放它?
来自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 achar *
. The newvoid *
type in Standard C makes these contortions unnecessary.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:
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.