We recently received a report that our application will occasionally fail to run. I tracked down the problem code to this:
struct ARRAY2D
{
long[] col;
}
int numRows = 800000;
int numCols = 300;
array = (ARRAY2D*) malloc(numRows * numCols * sizeof(long))
This allocation of 800 Mb can fail if the user doesn’t have a large enough free block. What is the best way to change how I allocate the memory?
Keep in mind that I have a large amount of code that accesses this object like this: array[row].col[colNum], so I need something that requires minor or primarily find & replace editing of the array access code.
You can allocate smaller chunks of memory separately, instead of one huge block.
Generally, memory allocation may fail, every allocation. However, let’s say statistically, due to memory fragmentation, allocating a single large block of memory has higher chance to fail more often than allocating N number of smaller blocks.
Although, also the solution above may cause problems as it is a bit like a double-bladed sword because it may lead to further memory fragmentation.
In other words, there is no generally perfect answer and solution depends on details of a system and application.
As from the comments it seems C++ library is a possibility, then solution based on
std::vector(i.e. generic vector of vectors in C++) or using Boost.MultiArray