c - 将堆存储从 const 转换为非常量会调用未定义的行为吗?
问题描述
标准 说:
(6.7.3) 如果尝试通过使用具有非 const 限定类型的左值来修改使用 const 限定类型定义的对象,则行为未定义。
在未定义的行为部分:
尝试通过使用具有非 const 限定类型的左值来修改使用 const 限定类型定义的对象 (6.7.3)
malloc的返回值是一块未初始化的存储。在该存储中没有构建任何对象。
也就是说,下面的代码合法吗?
#include
#include
typedef struct {
unsigned char h;
const unsigned int v;
} liber_primus;
int main(int argc, const char **argv)
{
unsigned int lykilord;
if (!(argc - 1))
return 0;
liber_primus *runaljod = malloc(sizeof (*runaljod));
if (!runaljod)
abort();
runaljod->h = *(unsigned char *)argv[1];
*(unsigned int *)&runaljod->v = 3307U;
lykilord = runaljod->h * runaljod->v;
free(runaljod);
return lykilord;
}
解决方案
推荐阅读
- django-rest-framework - 如果请求特定的 url,则更改模型字段
- java - 在我们的应用程序运行时面临 DDL 命令中的问题
- python-3.x - 无法使用 boto3 从 DynamoDB StringSet 中删除项目
- android - Camera2 MediaRecorder 改变 Galaxy S9 的帧速率
- mysql - csvkit mysql导入失败,VARCHAR需要方言mysql的长度
- python-3.x - 如何在 Python 的 Gtk 中通过 OpenCV 绘制图片
- symfony - Encore, Vue, Eslint - 使用最新的 vue-eslint-parser
- python - 如何解决未知属性axisbg
- java - Ιs there a way to use a class as a type of variable of another class with GSON?
- javascript - 使用 JavaScript 将一个数组按另一个数组分组