简体   繁体   English

结构内部的柔性阵列成员(c99)

[英]Flexible array member (c99) inside a structure

I've being using this code a while now, and it works fine, but it gave me some headache to implement it. 我现在已经在使用这段代码了一段时间,它可以正常工作,但是它使我有些头疼。 It uses Flexible Array Member (FAM) aka Struct Hack. 它使用灵活阵列成员(FAM)或Struct Hack。 Now that C99 has the possibility of using Variable Length Array (VLA), I wonder how can I take advantage in this piece? 既然C99可以使用可变长度数组(VLA),我想知道如何利用这一点?

typedef struct nO
{
    int oper;              /* some operator */
    int nops;              /* number of args */
    struct nO *ptn[1];     /* expansible array <=== HERE */
} nodoOper;

nodoOper *operator(int o, int n, ...)
{
    va_list ap;
    nodoOper *tn;
    size_t tam;
    int i;

    tam = sizeof(nodoOper) + (n - 1) * sizeof(nodoOper *); /* <=== AND HERE */

    if((tn=malloc(tam))==NULL)
        yyerror("Memory fault (cod. 4)");

    tn->oper = o;
    tn->nops = n;
    va_start(ap, n);
    for(i=0; i<n; i++)
        tn->ptn[i] = va_arg(ap, nodoOper*);
    va_end(ap);
    return tn;
}

(I've simplified the whole struct and code here, because it uses two more structs that is not important to the question, so maybe this example has bugs ) (我在这里简化了整个结构和代码,因为它使用了另外两个对这个问题并不重要的结构,所以这个示例可能有错误)

Now, the struct definition is in a header file (.h), and the code create a syntax tree for a compiler. 现在,结构定义位于头文件(.h)中,并且代码为编译器创建语法树。 How can I change the array to use variable length? 如何更改数组以使用可变长度?

Thanks! 谢谢! Beco. 贝乔。

Edited: rolling back the last editon to Flexible Array Member. 编辑:将最后一个编辑回退到Flexible Array Member。

2nd edition. 第2版​​。 Suppose I add inside the function this piece of code: 假设我在函数内添加以下代码:

struct nOp
{
    int oper;             /* some operator */
    int nops;             /* number of args */
    struct nOp *ptn[n];   /* Variable Length Array (VLA) */
};
struct nOp tnop;
struct nOp *tn2;

tn2 = &tnop;
return tn2;

First problem I see, I'm returning a pointer to a local variable. 我看到的第一个问题是,我正在返回一个指向局部变量的指针。 But beside that, is the way fruitful? 但是除此之外,这种方法是否富有成果? Thanks 谢谢

Actually, it's not variable-length arrays that you want to use here, but the struct hack, aka "incomplete types", aka the "flexible array member": 实际上,您不是要在这里使用可变长度数组,而是struct hack,也就是“ incomplete types”,又是“ flexible array member”:

typedef struct nO
{
    int oper;
    int nops;
    struct nO *ptn[];  // <== look ma, no index!
} nodoOper;

// skip a bit
// no more (n-1) --------\
tam = sizeof(nodoOper) + n * sizeof(nodoOper *);

Only the last member of a struct may be "flexible". 只有的最后一个成员struct可能是“灵活的”。

Variable-length arrays are a different feature: 可变长度数组是一个不同的功能:

void foo(int size)
{
    float a[size];               // run-time stack allocation
    printf("%lu\n", sizeof(a));  // and run-time sizeof (yuck)
}

(Oh, and these things are called arrays, not matrices.) (哦,这些东西叫做数组,而不是矩阵。)

The C name for that construction is "flexible array member" (it might help your searches), or "struct hack" before C99 . 该构造的C名称是“ flexible array member”(它可能有助于您的搜索), 或者是C99之前的“ struct hack”

Look at 6.7.2.1 in the Standard ; 参见标准 6.7.2.1; there is example usage in the text. 文本中有示例用法。


You may also be interested in "Variable Length Arrays". 您可能也对“可变长度数组”感兴趣。 See 6.7.5.2 in The Standard. 参见《标准》中的6.7.5.2。

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

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