On Wed, 30 May 2007 06:59:48 -0700, giddy <gi*******@gmail.comwrote:
hmm , took more than a few readings but i get it now. Thanks
Can i ask whether this is useful in anyway?
Mainly when exporting a structure (sending over the network, writing to
disk, using p/invoke and interop, etc.), to ensure that the layout is
consistent.
People might think they could use it internally to reduce memory usage (by
reducing the pack size), and in fact it might do that, but there could be
a significant performance hit on some hardware, and on other hardware it
may not even work due to unaligned access to the data (though I don't
believe this applies to anything .NET runs on). I wouldn't use it unless
I had a specific need to make sure the structure is laid out in a specific
way.
Also , i was looking to write binary files in the first place , or
more precisly write structures to a file.
I *think packing might come in handy , i wanted to write an invariable
amount of three different structures to a file. (something like a
little database) so i can use Pack to make all three structures the
same size and then write them? Is that how its done?
For writing structures to a binary file, packing is indeed useful.
Though, it's more about making sure a given structure is always the same,
as opposed to making different structures all the same size.
Was thinking of a header struct(or just a long?) that stores the
number of structures written into the file? So i would increment that
when everytime i add a new struct? Would this method prove inefficient
if i wanted to search through the structures?
If you have multiple types of structures that can be stored in the file,
you need some way of knowing what structures they are. Either they always
appear in the same order, or they include some sort of header themselves
that at a minimum identifies the type (via a numerical code or string or
whatever). If you know what structures they are, then you know what size
each structure is and there's no need to make all of the structures the
same size.
The only place I can see that the code might wind up signficantly simpler
is if you wanted to be able to skip over some number of structures without
actually looking to see what the structure type is. But for that to work
you'd have to have something that indexes into the list of structures.
Not impossible, but not sure how common that would be.
Note that many file formats actually encode the *length* of each structure
just before the structure itself. That way you don't really need to match
the structure type to a size, and you can even do versioning by adding new
fields to the structure but only ever reading the bytes that are actually
in the file. Traversing the file is easy with such a mechanism, since all
you have to do is look at the structure lengths written to the file. See
TIFF, RIFF, AVI (a special case of RIFF), and other formats for examples
of this.
Anyway, that's a long way of saying that packing is useful when writing
binary files that contain structures, but usually not for the purpose of
making all the structures the same size.
Pete