devxlogo

Static initialization and free store

Static initialization and free store

Question:
Is it valid to attempt to allocate (via ‘new()’) dynamic memory prior to ‘main()’? I am working on software that has static *’s to class objects, and through some rather nasty macro expansion, these object ptrs attempt to instantiate themselves via the ‘new()’ operator. I am working on Irix 6.2 and the software used to work fine under the Irix O32 ABI. When we ported to the N32 ABI, it started seg faulting and Purify reports IPR/IPW fatal errors throughout these static initializers.

Answer:
If I understand, you are declaring data outside of any functions something like this:

CData* pData = new CData[100];
In my experience (mostly in Visual C++ for Intel), this works fine. The data is, in fact, allocated, and this line executes before the call to main. I have never run across problems with this. If you are having trouble, it may be specific to the platform you’re porting to.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist