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

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语言写辅助基址是进程名,本人纯小白问下,知道基址,有辅助源码,怎么更新辅助中的基址...

    本帖最后由 ewq3391093 于 2016-8-28 20:27 编辑 首先本人纯小白问下,知道基址,有辅助源码,怎么更新辅助中的基址如题.再附上辅助源码http://pan.baidu.com/ ...

  3. linux c语言 glibc 错误 munmap,Linux内存分配小结--malloc、brk、mmap

    Linux的虚拟内存管理有几个关键概念: 1.每个进程都有独立的虚拟地址空间,进程访问的虚拟地址并不是真正的物理地址: 2.虚拟地址可通过每个进程上的页表(在每个进程的内核虚拟空间地址)与物理地址进行 ...

  4. linux 在指定区域分配内存 c语言,C语言动态内存分配:(一)malloc/free的实现及malloc实际分配/释放的内存...

    一.malloc/free概述 malloc是在C语言中用于在程序运行时在堆中进行动态内存分配的库函数.free是进行内存释放的库函数. 1.函数原型 #include void *malloc( s ...

  5. c语言怎么让写的函数兼容int型和char型_既然C语言void指针是“万能指针”,那么malloc还需类型转换吗?...

    在C语言程序开发中,动态内存分配是不可避免的,而调用 malloc()/free() 库函数实现这一过程是方便的,事实上,在很多C语言程序中,malloc/free 库函数的使用相当频繁,它俩的C语言 ...

  6. 程序设计工程师c语言,《C语言程序设计》自学百问.doc

    <C语言程序设计>自学百问.doc <C语言程序设计> 自学百问 张克军 前 言 为了便于同学们利用业余时间自学<C语言程序设计>课程,按照教学大纲和信息产业部的程 ...

  7. C语言动态内存分配:(一)malloc/free的实现及malloc实际分配/释放的内存

    最新个人博客 shankusu.me 以下内容转载或参考或引用自 https://blog.csdn.net/zxx910509/article/details/62881131 一.malloc/f ...

  8. 问下这个审核被拒怎么解决呢?——酷课堂iOS交流群问答整理(201806期)

    酷课堂iOS交流群问答整理(201806期) 以下内容由@客服妹子爬楼整理,希望小伙伴的这些提问,对你有帮助/启发,感谢资源贡献者:Mi~.appleMan.赖着.蒋哥.Rocky.群主.诚绅.思.其 ...

  9. 世界上最好语言:PHP技术百问

    世界上最好语言,PHP技术百问 曾经有个程序员段子,说:"PHP是世界上最好的语言"...如果你笑了,说明你是位真正的程序猿! 段子规段子,下面还是要严肃的来整理PHP相关的技术百 ...

最新文章

  1. 产生所有排列---旋转法------2013年1月22日
  2. 基因名2-MAR等错误名字产生原因
  3. MSSQL的多层嵌套查询
  4. OpenGL instancing实例化的实例
  5. Vue学习(组件传参)-学习笔记
  6. iOS coredata 多表查询
  7. 【ZJOI2019】线段树【线段树上dp】【大讨论】
  8. 交换机组播风暴_广播风暴的成因以及解决办法有哪些?
  9. Oralce的内存结构
  10. 静态路由实现负载均衡和高可用
  11. HTML的Demo实例
  12. 整型数字的英文表达和中文表达
  13. 解决谷歌浏览器最新chrome94版本CORS跨域问题(海康视频插件无法播放的问题)
  14. 计量经济学计算机第1章习题,计量经济学第1章习题.pdf
  15. 运动耳机哪些好用?专业运动耳机购买指南
  16. setsockopt 设置socket
  17. 用cat查看非常大非常大的文件是什么体验
  18. 计算机二级c常用算法,闽高校计算机二级C语言常用算法
  19. 微信公众号开发——现金红包、企业付款
  20. YEF思想秀:纳须弥于芥子,阐洞见于计算

热门文章

  1. LightGCN:用于推荐任务的简化并增强的图卷积网络 SIGIR 2020
  2. JAVA 线程的介绍与使用
  3. 4x3矩阵键盘扫描法c语言,四种4*4矩阵键盘的扫描方法
  4. centos 6.5 yum mysql 5.6_centos 6.5 yum安装 mysql 5.6
  5. java 析构方法_java析构方法详解
  6. ros云服务器_减少运维工作量,如何通过 ROS 轻松实现资源编排新方式
  7. 误删除Linux中libc.so.6文件急救办法
  8. 多进程event通信
  9. ASP.NET MVC铵钮Click后下载文件
  10. WebService学习总结(二)——WebService相关概念介绍