How to initialize static pointer with malloc in C?

血红的双手。 提交于 2019-12-18 04:47:10

问题


I'm trying to initiate a static variable (inside a function) with malloc in C, but I'm getting the "initializer not constant error". I know that I can't initiate a static with non constants in C, but can anyone think of a solution? I need the code to have the same effect as this:

static int *p = (int *)malloc(sizeof(int));

Is there a trick/workaround?

EDIT: I have a function that is called every time a flag goes high. In this function, I'm creating and starting a new thread. I declare a pointer to a struct and use malloc to allocate memory then pass this pointer to the thread. Then the function returns control. When I re-enter the function, the thread that I opened initially will still be running and I want to be able to access the memory region that I originally passed to the thread. That's why I need a static so that I can malloc on the first call and then use the same address on subsequent calls. This way I can get info from the thread. All this to avoid using global variables.


回答1:


Assuming you want function-static variables:

int foo(void) {
    static int b=1;
    static int *p;
    if (b) {
        p =  malloc(sizeof(int));
        b = 0;
    }
    ...
}

You can use NULL value for p as a check, as long as you know it will never be NULL after the first call.

Remember to check for errors in malloc; it is a runtime allocation, and should also be freed when it will not be needed anymore.




回答2:


static int *p = NULL;
if(!p) p = (int *)malloc(sizeof(int));



回答3:


malloc() is only used to allocate memory at runtime. Static variables are initialized at sompile time. You want:

static int p[1];



回答4:


If it is file static, then you should provide a public function in that file that will initialize that static.

void initialize () {
    if (p == 0) p = malloc(sizeof(*p));
}

Or, you can use a static function instead of a static variable. It costs you a check per access though:

static int * p () {
    static int * p_;
    return p_ ? p_ : (p_ = malloc(sizeof(*p_)));
}

For an integer type, this seems a little silly, but if p were some more complex type that required a more complicated initialization sequence than just the return value of malloc(), then it might make sense to have something like this.




回答5:


C can't do that. C++ can with static constructors.

You could do the allocation first thing in main(), or in any other function that is called before your pointer is needed.

While nonportable, some executable formats such as the Classic Mac OS' Code Fragment Manager support initialization/termination entry points. The CFM initialization was used for C++ static construction. If the executable format on your platform supports an initialization entry point, you could use that.



来源:https://stackoverflow.com/questions/16724801/how-to-initialize-static-pointer-with-malloc-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!