Tuesday, March 23, 2010

I love POC

POC stands for Plain Old C structure. A POC contains no references nor pointers, but only primitive value types or other POCs. No C++ elements (no user defined constructor, operator, etc)

The good of POC is that it is guaranteed to be allocated within a continuous memory space, so classic memory functions such as memcpy, memset works nicely on POC or array of POCs. Additionally, you get a free assignment operator, which is quite important for data types.

POC works very good with NSData for local file storage. Just copy the memory content of a POC into NSData and call writeToFile, saving the whole byte string to the file, easily done. If it's not a POC... well good luck manual-serializing fields of an instance while saving and de-serializing them while loading, don't get the order wrong! Of course, using POC you probably need to mind the debug/release settings, for the compiler may pad within POC, causing a different size between debug and release mode. Files written by application compiled under different modes may not compatible.

No comments:

Post a Comment