I disagree that you need to allocate the exactly right number of bytes. Any stream-based output system is likely to use malloc() or a container like std::vector<> for implementation, and those will certainly pad most size requests up a bit.
If you have an upper limit to the size of a serialized packet, then that's a fine, fixed, size to use for your serialization buffer, assuming that your code does the right thing (i e, fails loudly) if you try to exceed that size.
The bonus of keeping some number of pre-allocated, fixed-size blocks around is that you don't have to worry about fragmentation.
The rest of the advice is fine!
Show differencesHistory of post edits
#1hplus0603
Posted 15 June 2012 - 11:16 AM
I disagree that you need to allocate the exactly right number of bytes. Any stream-based output system is likely to use malloc() or a container like std::vector<> for implementation, and those will certainly pad most size requests up a bit.
If you have an upper limit to the size of a serialized packet, then that's a fine, fixed, size to use for your serialization buffer, assuming that your code does the right thing (i e, fails loudly) if you try to exceed that size.
The bonus of keeping some number of pre-allocated, fixed-size blocks around is that you don't have to worry about fragmentation.
If you have an upper limit to the size of a serialized packet, then that's a fine, fixed, size to use for your serialization buffer, assuming that your code does the right thing (i e, fails loudly) if you try to exceed that size.
The bonus of keeping some number of pre-allocated, fixed-size blocks around is that you don't have to worry about fragmentation.