简体   繁体   English

指向const静态数组的指针

[英]Pointer to const static array

For finite field math I store the corresponding addition and multiplication tables as statically typed integer arrays, eg, for GF(4/8), I have 对于有限域数学,我将对应的加法和乘法表存储为静态类型的整数数组,例如对于GF(4/8),我有

static const uint8_t GF4ADD[4][4] = {...};

static const uint8_t GF8ADD[8][8] = {...};

Now, at runtime, the program reads from a config file which field size is desired and should assign to a struct pointer the corresponding table: 现在,在运行时,程序将从配置文件中读取所需的字段大小,并应将对应的表分配给struct指针:

struct obj data {
...
  uint8_t** table_add;
  uint8_t** table_mult;
...
};

switch(order) {
case 4:
  data.table_add = GF4ADD;
  data.table_mult = GF4MULT;
  break;
case 8:
  data.table_add = GF8ADD;
  data.table_mult = GF8MULT;
  break;
}

Of course, the above doesn't work, but it should give you the idea of what I am trying to accomplish. 当然,上面的方法行不通,但是应该可以让您了解我要完成的工作。 The main problem is that I don't know of which type I should declare the struct members, as the size of the tables is only known at runtime. 主要问题是我不知道应该声明哪种类型的结构成员,因为表的大小仅在运行时才知道。 Besides, I do not want to resort to a one-dimensional indexing of the tables only. 此外,我不想只求表的一维索引。

Thanks, Tom. 谢谢,汤姆。

You need to declare it as you did, with pointers. 您需要像使用指针一样声明它。 After that, when you will get the order , you will be able to allocate dynamically the memory you will need. 之后,当您获得order ,您将能够动态分配所需的内存。


Simple example 1 (using pointer to pointer): 简单示例1(使用指向指针的指针):

struct{
    int ** table_add;
}data;


int main()
{
    unsigned int order;
    scanf("%u", &order);
    // create "order" pointers 
    data.table_add = malloc(order * sizeof(int *));
    // where each pointer points to "order" elements
    for(unsigned int i = 0; i < order; i++)
        data.table_add[i] = malloc(order * sizeof(int));

    // fill with numbers
    int counter = 0;
    for(unsigned int i = 0; i < order; i++)
        for(unsigned int j = 0; j < order; j++)
            data.table_add[i][j] = counter++;

    // read result
    for(unsigned int i = 0; i < order; i++){
        for(unsigned int j = 0; j < order; j++)
            printf("%d ",data.table_add[i][j]);
        printf("\n");
    }
    return 0;
}

Simple example 2 (allocating 2D array): 简单示例2(分配2D数组):

replace the start of the 1st example with: 将第一个示例的开头替换为:

    ...
    unsigned int order;
    scanf("%u", &order);
    // allocate memory for a 2D array
    data.table_add = malloc(sizeof(int[order][order]);
    // fill with numbers 
    ...

As @Lundin mentioned, this way is more efficient. 正如@Lundin所提到的,这种方式更有效。 The reasons for this are 3 as I see it: 我看到的原因是3:

  1. Size - no need to allocate memory for pointers (saves order * sizeof(int*) ) 大小 -无需为指针分配内存(保存order * sizeof(int*)
  2. Time - one call to malloc instead of order + 1 时间 -一次调用malloc而不是order + 1
  3. Simpler code (less mallocs --> less frees --> less errors) 简单的代码 (以下mallocs - >少frees - >少错误)

In your case, you will need to fill the elements in a loop (looping over each element and assigning them, or looping over order elements and using memcpy ) 在您的情况下,您需要将元素填充到一个循环中(在每个元素上循环并分配它们,或者在order元素上循环并使用memcpy

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

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