'Segfault when accessing static array from base class

I've got something like this:

template <typename Item>
class Base
{
public:
    Base(Item* arr, int size):
        m_data(arr),
        m_size(size)
    {
        for (int i = 0; i < size; ++i)
        {
            arr[i] = Item();
        }
    }
private:
    Item* m_data;
    int m_size;
};

template<typename Item, unsigned int Size=0>

class Derived
{
public:
    Derived():
        Base<Item>(m_storage, Size)
    {
    }
private:
    Item m_storage[Size]
};

So the reason I'm doing this is that I'm trying to initialize storage for use in the base class (a coverity complaint) and I've realized this is probably the wrong way to go about this, but I'm just wondering why am I getting a seg fault when the base class tries to write into the array. I'm initializing a size 3 derived with string as its item type and it segfaults in the first iteration of the for loop.

Also note, I think this is probably not the way to solve my coverity issue, and I have another solution for it, but I'm just really curious as to why it would seg fault since it should be able to access that static data in the array.

Thanks for the help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source