简体   繁体   English

结构初始化期间的静态数组初始化程序

[英]static array initializer during structure initialization

Here is a simplified version of two structs I have: 这是我拥有的两个结构的简化版本:

struct MyStruct1 {
    double d;
}

struct MyStruct2 {
    struct MyStruct1* a;
    int i;
}

I can initialize the second struct as follows: 我可以按如下方式初始化第二个结构:

void InitStruct(struct MyStruct2 pMyStruct2) {
    static struct MyStruct1 A[] = { {.d=12} , {.d=17} , {.d=1} };
    *pMyStruct2 = (struct MyStruct2) { .a = A, .i = 3 };
}

but actually I have to initialize it this way (its because this struct is again part of a bigger structure that shall be initialized at once): 但实际上我必须以这种方式对其进行初始化(它是因为此结构再次是应立即初始化的更大结构的一部分):

void InitStruct(struct MyStruct2 pMyStruct2) {
    *pMyStruct2 = (struct MyStruct2) { 
        .a = (struct MyStruct1[]) {
            {.d=12} , {.d=17} , {.d=1}},
        .i=3 };
}

Both ways compile without any warnings but the data in the second solution gets corrupted. 两种方式都可以编译,而不会发出任何警告, 但是第二个解决方案中的数据将被破坏。

I think that the inner array is not static and thus the .a -pointer gets invalid immediately. 我认为内部数组不是静态的,因此.a指针立即变为无效。

Is there another way to tell the compiler to keep the data of the array in the memory? 有没有其他方法可以告诉编译器将数组的数据保留在内存中?

C99 standard §6.5.2.5 p6 says that: C99标准 §6.5.2.5p6指出:

The value of the compound literal is that of an unnamed object initialized by the initializer list. 复合文字的值是由初始化程序列表初始化的未命名对象的值。 If the compound literal occurs outside the body of a function, the object has static storage duration; 如果复合文字出现在函数主体之外,则对象具有静态存储持续时间; otherwise, it has automatic storage duration associated with the enclosing block. 否则,它具有与封闭块关联的自动存储时间。

In both cases the varables should be properly initialized and valid for usage in the scope they are declared. 在这两种情况下,变量都应正确初始化,并在声明它们的范围内有效。

If you, however, return the structure from the function by value, the pointer will become invalid. 但是,如果按值从函数返回结构,则指针将变为无效。

Here is a sample code I used for testing. 这是我用于测试的示例代码。 Valgrind shows no errors. Valgrind没有显示任何错误。

#include <stdio.h>

struct MyStruct1 {
  double d;
};

struct MyStruct2 {
  struct MyStruct1* a;
  int i;
};

struct MyStruct1 A[] = { {.d=12} , {.d=17} , {.d=1} };
struct MyStruct2 b1 = { .a = A, .i = 3 };

struct MyStruct2 b2 = { .a = (struct MyStruct1[]) {
                                {.d=12} , {.d=17} , {.d=1}
                            },
                        .i=3 };



int main( void ) {
  struct MyStruct1 B[] = { {.d=12} , {.d=17} , {.d=1} };
  struct MyStruct2 b3 = { .a = B, .i = 3 };

  struct MyStruct2 b4 = { .a = (struct MyStruct1[]) {
                                {.d=12} , {.d=17} , {.d=1}
                            },
                          .i=3 };

  printf("b1->a.d=%1.2f\n", b1.a->d);
  printf("b2->a.d=%1.2f\n", b2.a->d);

  printf("b3->a.d=%1.2f\n", b3.a->d);
  printf("b4->a.d=%1.2f\n", b4.a->d);

}

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

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