The problem is the following:
I am storing error codes with IDs (e.g. 23,53,12,64...)
in an array with a predefined length (e.g. 10)
.
Now I would like to store some additional bytes to these codes
(e.g. **23** (data: 102, 340), **53** (data: 10), **12** (data: 46, 23, 64, 12), **64** (data: 1,2,3))
.
The data length is different for almost each error codes.
What is the best way of storing these additional bytes regarding the memory consumption?
My idea is to calculate the maximum memory I need for storing these additional bytes.
If I have 30 error codes and I would like to make it possible to store 10 of them (if they occur), then the maximum memory for storing the additional bytes can be calculated by summing up the number of the additional bytes of those error codes which require the most additional bytes. So when the 10 errors occur, which require the most additional bytes, the array for storing the additional bytes is just enough.
One error code only present once in a time.
When an error occurs, I save the additional bytes into this array and store a "pointer" (which is just an index of this array) to the error code, where the additional bytes start.
But this causes fragmentation if errors occur and I delete errors.
Defragmenting this array would be an overhead for the CPU.
Any ideas? I need to avoid dynamic memory allocating.