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

為什么 C++ 需要對 malloc() 進行強制轉換,而 C 不

Why does C++ require a cast for malloc() but C doesn#39;t?(為什么 C++ 需要對 malloc() 進行強制轉換,而 C 不需要?)
本文介紹了為什么 C++ 需要對 malloc() 進行強制轉換,而 C 不需要?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我一直對此很好奇 - 為什么在 C++ 中我必須從 malloc 轉換返回值,而不是在 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++ 中不起作用的示例(無強制轉換):

And here is the example in C++ that doesn't work (no cast):

int *int_ptr = malloc(sizeof(int*));

我聽說在 C 中,實際上,從 malloc() 轉換輸出是一個錯誤.

I heard that in C, in fact, casting an output from malloc() is a mistake.

有人可以評論這個話題嗎?

Can anyone comment on this topic?

推薦答案

幾點:

C 允許將 void 指針隱式轉換為任何其他對象指針類型.C++ 沒有.

C allows void pointers to be implicitly converted to any other object pointer type. C++ does not.

在 C 中轉換 malloc() 的結果將抑制有用的診斷,如果您忘記包含 stdlib.h 或沒有 malloc() 在范圍內.請記住,如果 C 看到一個沒有事先聲明的函數調用,它將假定該函數返回 int.如果您沒有對 malloc() 的聲明并且不進行強制轉換,您將得到一個診斷結果,表明您正在嘗試分配不兼容的類型(int 到指針).如果您轉換結果,則會抑制診斷,并且可能會出現運行時問題,因為不能保證將指針值轉換為 int 并再次轉換回指針會給您一個有用的結果.

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++,你應該使用 newdelete 而不是 malloc()free().是的,是的,是的,我聽說過人們希望他們的代碼同時編譯為 C 和 C++ 的所有原因,但是為該語言使用正確的內存管理工具的好處超過了維護兩個版本 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標準中添加的;早期版本的 C 有 malloc() 返回 char *,因此在這些版本中,如果您將結果分配給不同的結果,則需要 指針類型.不過,幾乎每個人都至少支持 C89 標準,因此您遇到這些舊實現之一的幾率非常非常低.

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.

這篇關于為什么 C++ 需要對 malloc() 進行強制轉換,而 C 不需要?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數?)
OpenCV with Network Cameras(帶有網絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 国产一区二区网站 | 在线免费播放av | 日本免费中文字幕 | 色婷婷精品 | 日韩免费在线视频 | 日韩av在线免费播放 | 国产精品久久一区二区三区 | 成人涩涩| 九九色 | 国产乱码一区二区 | 亚洲欧美日韩色图 | 谁有毛片网址 | 免费看黄色录像 | 午夜在线视频 | 亚洲一区中文字幕 | av一区二区三区在线观看 | h片在线免费观看 | 久久久久久久久久国产 | 午夜精品视频在线 | 国产又粗又猛视频免费 | 亚洲激情久久 | 日韩在线不卡 | 日本在线免费观看视频 | 亚洲福利网 | 欧美日韩二区三区 | a级片网站 | 天天操网站 | 激情网五月天 | 91久久精品日日躁夜夜躁欧美 | 久久性| 欧美视频免费 | 久久久精品在线 | 久久视频免费在线观看 | 成人免费在线视频观看 | 黑人精品xxx一区一二区 | 九九热在线观看 | 综合网在线 | 国产成人区 | 日韩av手机在线 | 国产区视频在线观看 | 福利视频一区二区 |