What is the best solution for static memory allocation?

I’m working on image processing and I need to use big Images in a critical system.

A good practice for critical systems is to avoid dynamic allocation of memory but what is the design/recommendations for static memory allocation?

If we do a static allocation of a big matrix we get stack overflow.

class test
{
    public:
        test(){};
        ~test(){};

    private:
        float m_bigMatrix[3000000];
};

To solve this problem we can define the matrix as static, but then this matrix will be the same for all the instance of this class.

static float m_bigMatrix[3000000];

The other solution is to have a static object.

static test t;

I’ve got a solution from another forum where it was suggested that the
best approach would be to use a class specific for memory management.

All the instances of the class are static and are used by other classes throughout the code.

Here is a sloppy version for this solution (it’s just an example):

class MemManager
{
public:
    MemManager(){ m_counter = 0u; }
    ~MemManager(){ printf("Out of MemManager nbr %dn", m_counter); }

    uint16_t getCounter() { return m_counter; }
    uint16_t getData(uint32_t pos) { return m_data[pos]; }

    void setCounter(uint16_t val) { m_counter = val; }
    void setData(uint16_t val, uint32_t pos) { m_data[pos] = val; }

private:
    static const uint32_t MEM_SIZE_DATA = 100000000u;
    uint16_t m_counter;
    uint16_t m_data[MEM_SIZE_DATA];

protected:
};

class MemUser
{
public:
    MemUser(){ m_memManager = NULL; }
    ~MemUser(){ printf("Out of MemUsern"); }

    void init(MemManager& memManager) { m_memManager = &memManager; };
    uint16_t getCounter() { return m_memManager->getCounter(); }
    uint16_t getData(uint32_t pos) { return m_memManager->getData(pos); }

    void setCounter(uint16_t val) { m_memManager->setCounter(val); }
    void setData(uint16_t val, uint32_t pos) { m_memManager->setData(val, pos); }

private:
    MemManager *m_memManager;

protected:
};

int main()
{
    static MemManager mManager;
    mManager.setCounter(1);
    {
        MemUser mUser;
        mUser.init(mManager);
        printf("Exit scopen");
    }
    for(uint8_t i = 1u; i<5u; i++)
    {
        static MemManager mManager2;
        printf("Note that mManager2 is the same: %dn", mManager2.getCounter());
        mManager2.setCounter(i);
    }
    printf("Exit processn");
    return 0;
}

So, is this correct? What is the best solution?

1

The whole point of avoiding dynamic memory allocation is that if your program has enough memory to complete initialization, then you know for a certainty you won’t hit an out of memory condition in the middle of your program, under some borderline set of conditions you hadn’t thought of.

Writing your own memory manager defeats that benefit. In fact, it makes it less safe. Instead of running out of heap with the built in memory manager that is highly tested, optimized, and used by millions of programmers, you are instead running out of memory on an untested memory manager, written by programmers who probably haven’t written a memory manager before, and used only by you.

If you’re getting stack overflows from your object being too large, the solution is to not allocate the object on the stack, as in your static test t; or static float m_bigMatrix[3000000];. Yes, it’s annoying. Yes, it goes against your instincts from what you’ve been taught are best practices on non-critical systems. That’s just how it goes.

One compromise I’ve seen is to only avoid dynamic deallocation, and make sure to perform all your dynamic allocation when the program first starts up. This makes it feel more natural, but still gives you the benefit of a known memory bound. However, it’s much more difficult to audit. When I worked before on critical systems, this was only allowed in very exceptional circumstances.

Images are normally stored uncompressed – typically 3 or 4 bytes per pixel, but possibly more. Therefore a 12 megapixel image takes maybe 48 megabytes of RAM. HDR etc might take rather more, but still not much in the big scheme of things.

I used to sort out these kind of budgets on early camera phones, when it was scarily much and we did all kinds of tricks to reduce it. But on today’s low-end phones, 48 MB is simply not much.

Don’t make complicated code. Just have a byte-array for the image. Focus on correctness. If you don’t have the RAM, then this isn’t a static memory allocation question but a compression or demand paging question and is a whole different level of complexity.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

What is the best solution for static memory allocation?

I’m working on image processing and I need to use big Images in a critical system.

A good practice for critical systems is to avoid dynamic allocation of memory but what is the design/recommendations for static memory allocation?

If we do a static allocation of a big matrix we get stack overflow.

class test
{
    public:
        test(){};
        ~test(){};

    private:
        float m_bigMatrix[3000000];
};

To solve this problem we can define the matrix as static, but then this matrix will be the same for all the instance of this class.

static float m_bigMatrix[3000000];

The other solution is to have a static object.

static test t;

I’ve got a solution from another forum where it was suggested that the
best approach would be to use a class specific for memory management.

All the instances of the class are static and are used by other classes throughout the code.

Here is a sloppy version for this solution (it’s just an example):

class MemManager
{
public:
    MemManager(){ m_counter = 0u; }
    ~MemManager(){ printf("Out of MemManager nbr %dn", m_counter); }

    uint16_t getCounter() { return m_counter; }
    uint16_t getData(uint32_t pos) { return m_data[pos]; }

    void setCounter(uint16_t val) { m_counter = val; }
    void setData(uint16_t val, uint32_t pos) { m_data[pos] = val; }

private:
    static const uint32_t MEM_SIZE_DATA = 100000000u;
    uint16_t m_counter;
    uint16_t m_data[MEM_SIZE_DATA];

protected:
};

class MemUser
{
public:
    MemUser(){ m_memManager = NULL; }
    ~MemUser(){ printf("Out of MemUsern"); }

    void init(MemManager& memManager) { m_memManager = &memManager; };
    uint16_t getCounter() { return m_memManager->getCounter(); }
    uint16_t getData(uint32_t pos) { return m_memManager->getData(pos); }

    void setCounter(uint16_t val) { m_memManager->setCounter(val); }
    void setData(uint16_t val, uint32_t pos) { m_memManager->setData(val, pos); }

private:
    MemManager *m_memManager;

protected:
};

int main()
{
    static MemManager mManager;
    mManager.setCounter(1);
    {
        MemUser mUser;
        mUser.init(mManager);
        printf("Exit scopen");
    }
    for(uint8_t i = 1u; i<5u; i++)
    {
        static MemManager mManager2;
        printf("Note that mManager2 is the same: %dn", mManager2.getCounter());
        mManager2.setCounter(i);
    }
    printf("Exit processn");
    return 0;
}

So, is this correct? What is the best solution?

1

The whole point of avoiding dynamic memory allocation is that if your program has enough memory to complete initialization, then you know for a certainty you won’t hit an out of memory condition in the middle of your program, under some borderline set of conditions you hadn’t thought of.

Writing your own memory manager defeats that benefit. In fact, it makes it less safe. Instead of running out of heap with the built in memory manager that is highly tested, optimized, and used by millions of programmers, you are instead running out of memory on an untested memory manager, written by programmers who probably haven’t written a memory manager before, and used only by you.

If you’re getting stack overflows from your object being too large, the solution is to not allocate the object on the stack, as in your static test t; or static float m_bigMatrix[3000000];. Yes, it’s annoying. Yes, it goes against your instincts from what you’ve been taught are best practices on non-critical systems. That’s just how it goes.

One compromise I’ve seen is to only avoid dynamic deallocation, and make sure to perform all your dynamic allocation when the program first starts up. This makes it feel more natural, but still gives you the benefit of a known memory bound. However, it’s much more difficult to audit. When I worked before on critical systems, this was only allowed in very exceptional circumstances.

Images are normally stored uncompressed – typically 3 or 4 bytes per pixel, but possibly more. Therefore a 12 megapixel image takes maybe 48 megabytes of RAM. HDR etc might take rather more, but still not much in the big scheme of things.

I used to sort out these kind of budgets on early camera phones, when it was scarily much and we did all kinds of tricks to reduce it. But on today’s low-end phones, 48 MB is simply not much.

Don’t make complicated code. Just have a byte-array for the image. Focus on correctness. If you don’t have the RAM, then this isn’t a static memory allocation question but a compression or demand paging question and is a whole different level of complexity.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật