如何在 C 中的结构中初始化 const(使用 malloc) [英] How to initialize const in a struct in C (with malloc)

查看:14
本文介绍了如何在 C 中的结构中初始化 const(使用 malloc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了;

void *malloc(unsigned int);
struct deneme {
    const int a = 15;
    const int b = 16;
};

int main(int argc, const char *argv[])
{
    struct deneme *mydeneme = malloc(sizeof(struct deneme));
    return 0;
}

这是编译器的错误:

gereksiz.c:3:17: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token

还有这个;

void *malloc(unsigned int);
struct deneme {
    const int a;
    const int b;
};

int main(int argc, const char *argv[])
{
    struct deneme *mydeneme = malloc(sizeof(struct deneme));
    mydeneme->a = 15;
    mydeneme->b = 20;
    return 0;
}

这是编译器的错误:

gereksiz.c:10:5: error: assignment of read-only member 'a'
gereksiz.c:11:5: error: assignment of read-only member 'b'

两者都没有被编译.使用 malloc 分配内存时,有没有办法在结构中初始化 const 变量?

And neither got compiled. Is there any way to initialize a const variable inside a struct when allocation memory with malloc?

推荐答案

你需要抛弃 const 来初始化 malloc 结构的字段:

You need to cast away the const to initialize the fields of a malloc'ed structure:

struct deneme *mydeneme = malloc(sizeof(struct deneme));
*(int *)&mydeneme->a = 15;
*(int *)&mydeneme->b = 20;

或者,您可以创建结构的初始化版本并对其进行 memcpy:

Alternately, you can create an initialized version of the struct and memcpy it:

struct deneme deneme_init = { 15, 20 };
struct deneme *mydeneme = malloc(sizeof(struct deneme));
memcpy(mydeneme, &deneme_init, sizeof(struct deneme));

如果你经常这样做,你可以将 deneme_init 设为静态和/或全局(所以它只需要构建一次).

You can make deneme_init static and/or global if you do this a lot (so it only needs to be built once).

使用 C11 标准参考解释为什么此代码不是某些评论所建议的未定义行为:

Explanation of why this code is not undefined behaviour as suggested by some of the comments, using C11 standard references:

  • 此代码不违反 6.7.3/6,因为 malloc 返回的空间不是具有 const 限定类型的对象定义";.表达式 mydeneme->a 不是一个对象,它是一个表达式.虽然它有const限定类型,但它表示一个没有用const限定类型定义的对象(实际上,根本没有用任何类型定义).

  • This code does not violate 6.7.3/6 because the space returned by malloc is not "an object defined with a const-qualified type". The expression mydeneme->a is not an object, it is an expression. Although it has const-qualified type, it denotes an object which was not defined with a const-qualified type (in fact, not defined with any type at all).

写入malloc 分配的空间永远不会违反严格的别名规则,因为每次写入都会更新有效类型 (6.5/6).

The strict aliasing rule is never violated by writing into space allocated by malloc, because the effective type (6.5/6) is updated by each write.

(但是,从 malloc 分配的空间中读取可能会违反严格的别名规则).

(The strict aliasing rule can be violated by reading from space allocated by malloc however).

在Chris的代码示例中,第一个将整数值的有效类型设置为int,第二个将有效类型设置为const int,但是在通过 *mydeneme 继续读取这些值的两种情况都是正确的,因为严格别名规则(6.5/7 项目符号 2)允许通过与有效类型相同或更多限定的表达式读取对象的对象.由于表达式mydeneme->a 的类型为const int,因此它可以用于读取有效类型intconst 的对象int.

In Chris's code samples, the first one sets the effective type of the integer values to int, and the second one sets the effective type to const int, however in both cases going on to read those values through *mydeneme is correct because the strict-aliasing rule (6.5/7 bullet 2) permits reading an object through an expression which is equally or more qualified than the effective type of the object. Since the expression mydeneme->a has type const int, it can be used to read objects of effective type int and const int.

这篇关于如何在 C 中的结构中初始化 const(使用 malloc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆