問(wèn)題描述
我一直對(duì)此很好奇 - 為什么在 C++ 中我必須從 malloc
轉(zhuǎn)換返回值,而不是在 C 中?
I have always been curious about this - why do in C++ I have to cast return value from malloc
but not in C?
以下是 C++ 中有效的示例:
Here is the example in C++ that works:
int *int_ptr = (int *)malloc(sizeof(int*));
這是 C++ 中不起作用的示例(無(wú)強(qiáng)制轉(zhuǎn)換):
And here is the example in C++ that doesn't work (no cast):
int *int_ptr = malloc(sizeof(int*));
我聽(tīng)說(shuō)在 C 中,實(shí)際上,從 malloc()
轉(zhuǎn)換輸出是一個(gè)錯(cuò)誤.
I heard that in C, in fact, casting an output from malloc()
is a mistake.
有人可以評(píng)論這個(gè)話題嗎?
Can anyone comment on this topic?
推薦答案
幾點(diǎn):
C 允許將 void 指針隱式轉(zhuǎn)換為任何其他對(duì)象指針類型.C++ 沒(méi)有.
C allows void pointers to be implicitly converted to any other object pointer type. C++ does not.
在 C 中轉(zhuǎn)換 malloc()
的結(jié)果將抑制有用的診斷,如果您忘記包含 stdlib.h 或沒(méi)有 malloc()
在范圍內(nèi).請(qǐng)記住,如果 C 看到一個(gè)沒(méi)有事先聲明的函數(shù)調(diào)用,它將假定該函數(shù)返回 int
.如果您沒(méi)有對(duì) malloc()
的聲明并且不進(jìn)行強(qiáng)制轉(zhuǎn)換,您將得到一個(gè)診斷結(jié)果,表明您正在嘗試分配不兼容的類型(int 到指針).如果您轉(zhuǎn)換結(jié)果,則會(huì)抑制診斷,并且可能會(huì)出現(xiàn)運(yùn)行時(shí)問(wèn)題,因?yàn)椴荒鼙WC將指針值轉(zhuǎn)換為 int 并再次轉(zhuǎn)換回指針會(huì)給您一個(gè)有用的結(jié)果.
Casting the result of malloc()
in C will supress a useful diagnostic if you forget to include stdlib.h or otherwise don't have a declaration for malloc()
in scope. Remember that if C sees a function call without a prior declaration, it will assume that the function returns int
. If you don't have a declaration for malloc()
and you leave off the cast, you'll get a diagnostic to the effect that you're trying to assign incompatible types (int to pointer). If you cast the result, you supress the diagnostic and will potentially have runtime issues, since it's not guaranteed that converting a pointer value to an int and back to a pointer again will give you a useful result.
如果你在寫 C++,你應(yīng)該使用 new
和 delete
而不是 malloc()
和 free()代碼>.是的,是的,是的,我聽(tīng)說(shuō)過(guò)人們希望他們的代碼同時(shí)編譯為 C 和 C++ 的所有原因,但是為該語(yǔ)言使用正確的內(nèi)存管理工具的好處超過(guò)了維護(hù)兩個(gè)版本 IMO 的成本.
If you're writing C++, you should be using new
and delete
instead of malloc()
and free()
. Yeah, yeah, yeah, I've heard all the reasons why people want their code to compile as both C and C++, but the benefits of using the right memory management tool for the language outweigh the cost of maintaining two versions IMO.
注意:void *
類型是在C89標(biāo)準(zhǔn)中添加的;早期版本的 C 有 malloc()
返回 char *
,因此在這些版本中,如果您將結(jié)果分配給不同的結(jié)果,則需要 指針類型.不過(guò),幾乎每個(gè)人都至少支持 C89 標(biāo)準(zhǔn),因此您遇到這些舊實(shí)現(xiàn)之一的幾率非常非常低.
Note: the void *
type was added in the C89 standard; earlier versions of C had malloc()
return char *
, so in those versions the cast was required if you were assigning the result to a different pointer type. Almost everybody supports at least the C89 standard though, so the odds of you running into one of those older implementations is very, very low.
這篇關(guān)于為什么 C++ 需要對(duì) malloc() 進(jìn)行強(qiáng)制轉(zhuǎn)換,而 C 不需要?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!