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

        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
    MemManager(){ m_counter = 0u; }
    ~MemManager(){ printf("Out of MemManager nbr %d\n", 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; }

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


class MemUser
    MemUser(){ m_memManager = NULL; }
    ~MemUser(){ printf("Out of MemUser\n"); }

    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); }

    MemManager *m_memManager;


int main()
    static MemManager mManager;
        MemUser mUser;
        printf("Exit scope\n");
    for(uint8_t i = 1u; i<5u; i++)
        static MemManager mManager2;
        printf("Note that mManager2 is the same: %d\n", mManager2.getCounter());
    printf("Exit process\n");
    return 0;

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

  • I had to implement a dynamic memory allocator using linked lists of arrays (in order to allow potentially large amounts of data), and in order to ensure the memory was available, we had to actually write the arrays (otherwise the operating system would actually lazy-load your memory allocation). I wouldn't recommend that type of approach to anyone given how complicated it was, but maybe that could help you in some way.
    – Neil
    Jul 29, 2013 at 16:07

2 Answers 2


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.

  • Thanks Will and Neil for your help. The problem was proposed (or imposed) as a safe development approach. The idea is that if we use static memory and if something is wrong, the program will crash at the first run or/and will not pass the test phase. Having a static matrix in a class having the class itself defined as static are not good options. The solution of having a class managing all used memory seems to be the best approach, I just hope that don't get into troubles later and end up having to change my code. KISS approach, yes this is a Memory Pool problem.
    – jblasius
    Jul 29, 2013 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.