久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么 C++ 需要對(duì) malloc() 進(jìn)行強(qiáng)制轉(zhuǎn)換,而 C 不

Why does C++ require a cast for malloc() but C doesn#39;t?(為什么 C++ 需要對(duì) malloc() 進(jìn)行強(qiáng)制轉(zhuǎn)換,而 C 不需要?)
本文介紹了為什么 C++ 需要對(duì) malloc() 進(jìn)行強(qiáng)制轉(zhuǎn)換,而 C 不需要?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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)該使用 newdelete 而不是 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺(tái)輸出?)
How do I calculate the week number given a date?(如何計(jì)算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡(luò)攝像機(jī)的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時(shí)導(dǎo)出所有符號(hào))
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 免费1区2区3区 | 国产精品一区二区视频 | 日本午夜精品一区二区三区 | 拍真实国产伦偷精品 | 久久久久久免费毛片精品 | 久久国产高清视频 | 亚洲精品一区二区 | 一区福利视频 | 国产精品久久久久久久久久免费看 | 亚洲欧美日韩在线一区二区 | 先锋av资源在线 | 久久99精品国产自在现线小黄鸭 | ww 255hh 在线观看 | 久久久无码精品亚洲日韩按摩 | 国产一区亚洲 | 99久久久无码国产精品 | 亚洲成人精品 | 亚洲精品国产第一综合99久久 | 欧美日韩视频 | 亚洲一区二区在线 | 亚洲一一在线 | 在线免费看黄 | 国产第1页| 国产一区免费 | 色综合久 | 国产一区二区在线免费视频 | 欧美va大片 | 激情综合五月 | 国产精品免费一区二区三区四区 | 亚州精品天堂中文字幕 | 国产小视频在线观看 | 日韩成人精品在线观看 | 亚洲 欧美 综合 | 日韩一区二区三区在线观看 | 99色视频| 国产美女久久久 | 亚洲天天干 | 久久精品小视频 | 色一阁 | 操久久| 韩国av一区二区 |