Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static data in header-only libraries

I am developing a library that will consist of header files only. So far, it contains only classes, which has been fine. However, I have come to a point where I need to have some library-wide accessible unchanging data in the library (that is, not class instance data) for the implementation of some functions. You obviously can't just put global data in header files, or else every compilation unit that #includes the header will have a definition for the symbol and you'll get multiple definition errors at link-time.

I seem to have found a workaround that lets me have static data in a class without having to add a compilation unit to the library by just making the data a static variable in a function and returning a pointer to that data:

class StaticData {
public:
    void doSomething() { /* this uses getData */ }
    void doSomethingElse() { /* this does too */ }

private:
    static int* getData() {
        static int array[] { 1, 2, 3, 4 };

        return array;
    }
};

This appears to be working fine, but I must admit that I don't know what happens to function-static data in inline functions in header files. I am wondering if this "hack" has any unintended repercussions, such as every compilation unit that #includes this header getting its own version of array. How and where does the compiler decide to put it?

Also it should be noted that I am not using this to implement the singleton antipattern or anything. I am just using it to store data that multiple functions will need to use (which is why it can't be static in just a function that uses it, but even if it did, that would prompt the same question).

like image 366
Seth Carnegie Avatar asked Sep 06 '12 17:09

Seth Carnegie


People also ask

What happens if static variable is declared in header file?

When defining a static variable in a header file, a new instance of the variable is created for each file including the header file. This is often surprising as people often expect to have only one instance of the variable. This leads to errors that are very difficult to track/understand.

Can we use static in header file?

Yes, It can be declared. It will be considered as a global static variable. It will limit its scope within that file in which that header file has been included.

Can we initialize static variable in header file?

When a static value is used to initialize another static variable, the first may not be initialized, yet. // file. h class File { public: static struct _Extensions { const std::string h{ ". h" }; const std::string hpp{ ".

Where static variables are stored?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).


1 Answers

That's fine. It's guaranteed that there will only be one copy of array, as long as the function has external linkage, which this does. The C++ standard says:

7.1.2/4 A static local variable in an extern inline function always refers to the same object.

like image 196
Mike Seymour Avatar answered Sep 20 '22 10:09

Mike Seymour