该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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的声明问题相关推荐

  1. c语言malloc和直接声明,问下关于malloc的声明问题

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 Advantages to casting Including the cast allows a program or function to comp ...

  2. C语言中squeeze的作用,pytorch下的unsqueeze和squeeze的用法说明

    #squeeze 函数:从数组的形状中删除单维度条目,即把shape中为1的维度去掉 #unsqueeze() 是squeeze()的反向操作,增加一个维度,该维度维数为1,可以指定添加的维度.例如u ...

  3. C语言 SLEEP函数不起作用,C语言中的sleep() 函数 sleep函数的具体用法

    导航:网站首页 > C语言中的sleep() 函数 sleep函数的具体用法 C语言中的sleep() 函数 sleep函数的具体用法 相关问题: 匿名网友: 关于sleep()函数在windo ...

  4. go语言中channel的创建和销毁以及匿名函数的使用

    channel的创建 go语言中,任意类型前面加上关键字chan即可声明对应类型的通道,创建通道需要使用make,make也用于map 和slice的创建 创建一个通道 /*刚创建的通道是nil*/ ...

  5. c语言中%加字母是啥意思,%C是什么意思? C语言中%c,%s分别代表什么意思?

    导航:网站首页 > %C是什么意思? C语言中%c,%s分别代表什么意思? %C是什么意思? C语言中%c,%s分别代表什么意思? 相关问题: 匿名网友: %c单个字符输出的意思. %s是输出字 ...

  6. malloc 初始化_在C语言中,请一定记得初始化局部变量!

    C语言中,未初始化的局部变量到底是多少? 答案往往是: 与编译器有关. 可能但不保证初始化为0. 未确定. 总之,全部都是些一本正经的形而上答案,这很令人讨厌. 但凡一些人给你滔滔不绝地扯编译器,C库 ...

  7. C语言中malloc与free

    一.为什么c语言中要有malloc malloc就是memory allocate动态分配内存,malloc的出现时为了弥补静态内存分配的缺点,静态分配内存有如下缺点: 1.比如说,传统的一维数组,如 ...

  8. 关于C语言中的malloc和free函数的用法

    一.malloc()和free()的基本概念以及基本用法: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针 ...

  9. c语言36块砖36人搬答案编程,在C语言中.36块砖,36人搬;男搬4,女搬3,两个小孩抬一块.要求一次搬完,问男女小孩各多少?设计程序....

    题目: 在C语言中.36块砖,36人搬:男搬4,女搬3,两个小孩抬一块.要求一次搬完,问男女小孩各多少?设计程序. 为什么男人的数不能取到9,而取到8:女人的数不能取到12,而取到11. 解答: 为什 ...

最新文章

  1. 小程序 reduce_使用Reduce制作的10个JavaScript实用程序功能
  2. 使用PL/SQL Developer远程连接到oracle11g
  3. python装饰器实例-Python装饰器实用例子
  4. 处理微信文章中防盗链问题
  5. 【问链财经-区块链基础知识系列】 第三十二课 从区块链溯源来看农产品链的设计
  6. 【Python】pandas 1.3版本主要更新内容一览
  7. 14英寸电脑长宽多少_首款5G折叠屏电脑发布:看完价格,我酸了
  8. 【转载】在C#中运用SQLDMO备份和恢复Microsoft SQL Server数据库
  9. 13攻击行为是天生的
  10. python 多线程同步_Python利用多线程同步锁实现多窗口订票系统(推荐)
  11. 字节缓冲流 BufferedInputStream java
  12. 页面布局_【三十七集】页面布局中的视觉流
  13. 苹果服务器修改,82559修改MAC及服务器版本网卡教程!
  14. 红外图像盲元检测matlab,一种红外图像盲元检测方法与流程
  15. 使用谷歌浏览器自带的谷歌翻译提示“无法翻译此网页”
  16. 神经网络算法用什么软件,神经网络计算机应用
  17. VMware虚拟机系统没有声音?
  18. 黎明职业大学计算机教研室主任,黎明职业大学计算机系相关专业.doc
  19. 政治冲刺押题所有“黑幕”!只用肖四肖八行吗?
  20. 2022年焊工(初级)考试模拟100题模拟考试平台操作

热门文章

  1. why there is error after adding DocumentHistories
  2. Appointment search in Fiori and Webui
  3. 我们在tool里给ui element设置断点,然后操作的时候,断点就触发了。Framework是咋实现的
  4. 如何从WebSocket服务器收到的数据判断出有哪些客户端需要广播
  5. 如何处理错误消息Query XXX is invalid or contains errors
  6. 使用命令行工具mc上传模型训练数据到SAP云平台Leonardo机器学习服务的AWS存储
  7. 在浏览器里使用SAPGUI里的SE80
  8. 方舟手游服务器设置文件翻译,方舟生存进化手游界面翻译 方舟生存进化手机版中文对照翻译一览...
  9. bootstrape常用标签_bootstrap_03_常用标签
  10. matlab读取fortran文件夹,怎样用fortran获取一个文件夹里的全部文件名?