久久久久久久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 入門)
主站蜘蛛池模板: 成人三级视频 | 免费啪视频 | 高清一区二区三区 | 日韩黄色影院 | 国产精品毛片一区视频播 | 色在线视频 | 中文字幕激情 | 国产在线一区二区 | 国产成人免费在线 | 香蕉视频一区二区 | 色香蕉视频 | 伊人久久影院 | 日本激情视频 | 亚洲成人欧美 | 亚洲福利网 | 在线观看黄色片 | 在线观看黄网站 | 国产欧美一区二区精品性色超碰 | 伊人网综合 | 一级黄色片在线观看 | 97精品在线视频 | 性色av一区二区三区 | 日本丰满少妇做爰爽爽 | 一级特黄视频 | 激情综 | 精品黄色| 欧美做爰xxxⅹ性欧美大片 | 欧美黄色录像 | 91看片在线观看 | 在线观看视频国产 | 欧美成人综合 | 欧美国产在线视频 | 91在线免费视频 | 黄色大片免费观看 | 99久久久国产精品免费蜜臀 | 亚洲少妇视频 | 亚洲一区在线视频 | 国产天堂在线观看 | 日本激情网站 | av福利在线 | 人人干人人爽 |