在C语言中malloc怎么声明,问下关于malloc的声明问题
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
Advantages to casting
Including the cast allows a program or function to compile as C++.
The cast allows for pre-1989 versions of malloc that originally returned a char *.[8]
Casting can help the developer identify inconsistencies in type sizing should the destination pointer type change, particularly if the pointer is declared far from the malloc() call.[edit]Disadvantages to casting
Under the ANSI C standard, the cast is redundant.
Adding the cast may mask failure to include the header stdlib.h, in which the prototype for malloc is found.[8][9] In the absence of a prototype for malloc, the standard requires that the C compiler assumemalloc returns an int. If there is no cast, a warning is issued when this integer is assigned to the pointer; however, with the cast, this warning is not produced, hiding a bug. On certain architectures and data models (such as LP64 on 64-bit systems, where long and pointers are 64-bit and int is 32-bit), this error can actually result in undefined behaviour, as the implicitly declared malloc returns a 32-bit value whereas the actually defined function returns a 64-bit value. Depending on calling conventions and memory layout, this may result in stack smashing. This issue is less likely to go unnoticed in modern compilers, as they uniformly produce warnings that an undeclared function has been used, so a warning will still appear. For example, GCC's default behaviour is to show a warning that reads "incompatible implicit declaration of built-in function" regardless of whether the cast is present or not.
If the type of the pointer is changed, one must fix all code lines where malloc was called and cast (unless it was cast to a typedef).
總結
以上是生活随笔為你收集整理的在C语言中malloc怎么声明,问下关于malloc的声明问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 流媒体商 Roku 旗下 Select
- 下一篇: 酷狗音乐锁屏怎么关掉