简体   繁体   English

具有静态const成员的动态分配数组

[英]Dynamically allocated array with static const members

How can one define and use a dynamically allocated array, whose members are static const ? 一个如何定义和使用成员为static const的动态分配数组?

Background: I need to do the above, to store the several transaction that are requested at runtime. 背景:我需要执行上述操作,以存储运行时请求的几个事务。 The snipet bellow exemplifies how to define a transaction. 鸣叫框说明了如何定义交易。 This code uses the Nordic Semiicondictor nRF5x SDK. 此代码使用Nordic Semiicondictor nRF5x SDK。

static app_twi_transfer_t const transfers[] =
{
    APP_TWI_WRITE(MMA7660_ADDR, p_reg_addr, 1, APP_TWI_NO_STOP), 
    APP_TWI_READ (MMA7660_ADDR, p_buffer,   byte_cnt, 0)
};

static app_twi_transaction_t const transaction =
{
    .callback            = read_mma7660_registers_cb,
    .p_user_data         = NULL,
    .p_transfers         = transfers,
    .number_of_transfers = sizeof(transfers)/sizeof(transfers[0])
};

APP_ERROR_CHECK(app_twi_schedule(&m_app_twi, &transaction));

How can one define and use a dynamically allocated array, whose members are static const? 一个如何定义和使用成员为静态const的动态分配数组?

You can't. 你不能 The members of an array necessarily have the same storage class and linkage as the array itself, therefore a dynamically-allocated array cannot have static members. 数组的成员必须具有与数组本身相同的存储类和链接,因此动态分配的数组不能具有静态成员。 Such an array can, however, have copies of or pointers to objects with static storage class and/or linkage. 但是,这样的数组可以具有静态存储类和/或链接的对象的副本指针

You cannot statically initialize members of a dynamically allocated array: the only two options supplied by the standard library are uninitialized , ie malloc , and zero-initialized , ie calloc . 您不能静态初始化动态分配的数组的成员:标准库提供的仅有两个选项是未初始化的 ,即malloc零初始化的 ,即calloc

If you would like to initialize elements of your array to anything else, you need to perform assignments yourself. 如果您想将数组的元素初始化为其他任何元素,则需要自己执行分配。 C lets you assign struct s directly, so initializing an array of struct s is not much different from initializing an array of primitives. C使您可以直接分配struct ,因此初始化struct数组与初始化基元数组没有太大区别。

Here is a small example: 这是一个小例子:

// This is your struct type
typedef struct {
    int a;
    int b;
    int c;
} test_t;
// This is some statically initialized data
test_t data[] = {
    {.a=1, .b=2, .c=3}
,   {.a=10, .b=20, .c=30}
,   {.a=100, .b=200, .c=300}
};
int main(void) {
    // Allocate two test_t structs
    test_t *d = malloc(sizeof(test_t)*2);
    // Copy some data into them:
    d[0] = data[1];
    d[1] = data[2];
    // Make sure that all the data gets copied
    printf("%d %d %d\n", d[0].a, d[0].b, d[0].c);
    printf("%d %d %d\n", d[1].a, d[1].b, d[1].c);
    free(d);
    return 0;
}

What looks like regular assignments above, eg d[0] = data[1] , performs a copy of the content of statically initialized data[1] into dynamically initialized d[0] . 上面看起来像常规分配的d[0] = data[1] ,例如d[0] = data[1] ,将静态初始化的data[1]的内容复制到动态初始化的d[0]

Demo. 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM