I have written a 3 dimensional C implementation of the Game of Life by John Conway. Now, I want to make the code safe for errors. This article from yolinux.com says
Check for memory allocation errors. Can't free it if it didn't get allocated.
My question is, should I really check for allocation errors every time? That could immensively slow down my code. Here is an excerpt:
inline int golField_Init(golField* field, golAllocProc alloc, golOptions options, uint dimx, uint dimy, uint dimz) { field->dimx = dimx; field->dimy = dimy; field->dimz = dimz; field->cells = alloc(sizeof(golCell**) * dimx); field->options = options; if (!field->cells) return 1; int x, y, z; for (x = 0; x < dimx; ++x) { field->cells[x] = alloc(sizeof(golCell*) * dimy); for (y = 0; y < dimy; ++y) { field->cells[x][y] = alloc(sizeof(golCell) * dimz); for (z = 0; z < dimz; ++z) { golCell_Init( &field->cells[x][y][z], 0 ); } } } return 0; } Should I really check for allocation errors on each allocation? That would also require me to free already allocated storage.
#define GOL_3DTOLINEAR(x, y, z, dx, dy, dz) ( (z) + ((y) * (dz)) + ((x) * (dz) * (dy)) )