How to initialize static pointer with malloc in C?

后端 未结 5 597
臣服心动
臣服心动 2020-12-17 22:27

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 stat

5条回答
  •  自闭症患者
    2020-12-17 23:16

    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.

提交回复
热议问题