开发者

Defining a value of a struct in it's declaration in a header file

Please forgive me if this is a dumb question, I'm fairly new to C and couldn't find an example of this online so I assume I cant do what I want. but, hopefully someone here can point me into the right direction.

so I have a headerfile that declares a struct like so

typedef struct{

    float *float_array1;

    float *float_array2;

    int anInt;

    int anotherInt;

}IMAStruct;

IMAStruct aStruct;

I would li开发者_开发问答ke anInt to always be initialized to 0 when the struct is declared, the mem for the float arrays gets allocated based on whether it is 1 or 0;

The problem is I'm working on a massive project and this code is used in an action listener, and I really cant modify any other part of the code. I only want it to run the code once and then skip over it. This seemed like a good way to do it.


This isn't the type of thing you should do in a header.

The issue is that the declaration of the struct does not execute any code. Calling malloc or making a conditional branch requires executing code.

Now, you can make it happen anyhow in GCC by declaring a function like this:

void initAStruct __attribute__((init)) {
    aStruct.anInt = 0;
    aStruct.float_array2 = (float*)malloc(sizeof(float)*10);
}

In C99, I think this can be done with float float_array2[anInt*10];, but you can't do that in C89.


I'm not sure quite what you want, but as this is C and not C++, you'll need to write a function to initialize your structures the way you want. There's no way in C to specify that every instance of a structure starts with a particular initialization. You could define a constant initializer (as a macro) and require that all users of the struct initialize it with that, but it won't work when you need dynamic allocation.


If the structure is defined at file scope, i.e. outside any function, (or in a function if it is static,) then it is always initialized in the same way: integers and floating point fields are initialized to 0, and pointer fields are initialized to NULL (this applies recursively to nested structures and unions).

If the structure is defined in a function (“on the stack”, or allocated “in the heap” with malloc), the fields are not initialized automatically, and they start by containing whatever random garbage happened to be in these bits of memory beforehand.

There is no portable way to specify program initialization code; if you need to run something when your program starts, you have to put it in main. Some platforms do provide non-portable extensions for this however.


Just initialize it everywhere regardless whether in file or function scope like this:

IMAStruct aStruct = { 0 };

This guarantees that all fields are initialized to 0 or NULL for the pointers. Then you may easily detect if a variable has not yet been initialized and trigger an initialization.

In the file scope such an initialization is redundant, but I think it is easier to stick to the same initialization rule everywhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜