0

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.

3
  • 3
    Checking whether a pointer is null is blindingly fast on any modern CPU. Commented Mar 11, 2012 at 21:11
  • 2
    Suggestion - You could consider doing a single array allocation upfront so you don't have to do one for every row/column. That is "field->cells = malloc(sizeof(golCell)*dimxdimydimz)" Commented Mar 12, 2012 at 4:50
  • selbie: That's what I did when I tried to do it n-dimensional, but that was wayy to complex. Then I came up with the "3d code" from above, and then, after reading the answers, with a damn complex way to create a 3d array but allocating the storage in 3 steps and finally I'm now at the point that you suggested, ha. xD For anyone who searches for a way to access and element in a pseudo 3d array mapped linearly: #define GOL_3DTOLINEAR(x, y, z, dx, dy, dz) ( (z) + ((y) * (dz)) + ((x) * (dz) * (dy)) ) Commented Mar 12, 2012 at 14:32

3 Answers 3

3

It will not massively slow down your code. Yes, check all allocations and exit graciously if you can't allocate.

Yes, it will also require you to free memory you allocated. Welcome to C :)

If you want to not think less about allocations, use language with garbage collector and exceptions. First allows you to not free memory yourself. Second lets you catch all errors in one place, if you can't allocate memory (unless it's not what you want to do, because for example memory was allocated in part of a code that is optional).

Sign up to request clarification or add additional context in comments.

Comments

2

Should I really check for allocation errors on each allocation?

Yes, you should.

That would also require me to free already allocated storage.

No, you should do that in any case, regardless of whether you check for errors.

If you're afraid of a performance drop, then do memory management outside of your inner loops. Since you're doing it in an initialization function now, there should be no problem. Error checking itself is very cheap compared to the cost of calling a memory allocator.

2 Comments

Among this implementation, how should I implement the memory management outside of the loops? Thanks.
@NiklasR: given that the function's name ends in _Init, I assume it's already outside of your processing loops. But you could still simplify the error handling and speed up your program by allocating a single array instead of arrays of pointers.
1

short answer: ABSOLUTELY YES

long answer:

depending on what you are implementing you could for example allocate the needed memory at once... which would be better regarding perofrmance etc.

BUT ALWAYS CHECK FOR ALLOCATION ERRORS AND ALWAYS FREE WHAT YOU ALLOCATED (ONLY ONCE!).

Failing to do so result in any of several negative aspects: accessing null pointers, memory leaks etc. - the stuff instable and vulnerable software is made of...

1 Comment

I like the idea of allocating the storage at once. Just thinking of how to implement it without the need of computing the linear-index of the 3-d cell I'd like to access. I mean, I don't want to access the "3d array" like cells[10].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.