Yes, you will probably need to do this using malloc()
, and here's why:
When any program (process ... thread ...) is started, it is given a chunk of memory which it uses to store (among other things ...) "local" variables. This area is called "the stack." It most-certainly won't be big enough to store 16 megabytes.
But there's another area of memory which any program can use: its "heap." This area (as the name, "heap," is intended to imply ...) has no inherent structure: it's simply a pool of storage, and it's usually big enough to store many megabytes. You simply malloc()
the number of bytes you need, and free()
those bytes when you're through.
Simply define a type
that corresponds to the structure you need to store, then malloc(sizeof(type))
. The storage will come from the heap. (And that, basically, is what the heap is for ...)
Incidentally, there's a library function called calloc()
which will reserve an area that is "known zero." Furthermore, it might use clever operating-system tricks to do so very efficiently.