c - What is the best approach when working with on-disk data structures -


i know how best work on-disk data structures given storage layout needs match logical design. find structure alignment & packing not when need have layout storage.

my approach problem defining (width) of structure using processor directive , using width when allocation character (byte) arrays write disk after appending data follows logical structure model.

eg:

typedef struct __attribute__((packed, aligned(1))) foo {    uint64_t some_stuff;    uint8_t flag; } foo; 

if persist foo on-disk "flag" value come @ end of data. given can use foo when reading data using fread on &foo type using struct without further byte fiddling.

instead prefer

#define foo_width sizeof(uint64_t)+sizeof(uint8_t)  uint8_t *foo = calloc(1, foo_width);  foo[0] = flag_value; memcpy(foo+1, encode_int64(some_value), sizeof(uint64_t)); 

then use fwrite , fread commit , read bytes later unpack them in order use data stored in various logical fields.

i wonder approach best use given desire layout of on-disk storage match logical layout ... example ...

if knows how efficient each method respect decoding/unpacking bytes vs copying structure directly it's on-disk representation please share , prefer using second approach since gives me full control on storage layout not ready sacrifice looks performance since approach requires lot of loop logic unpack / traverse through bytes various boundaries in data.

thanks.

based on requirements (considering looks , performance), first approach better because, compiler hard work you. in other words, if tool (compiler in case) provides feature not want implement on own because, in cases, tool's implementation more efficient yours.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -