Thursday, March 31, 2011

Read data with varying formats in C++

I'm creating my first real binary parser (a tiff reader) and have a question regarding how to allocate memory. I want to create a struct within my TiffSpec class for the IFD entries. These entries will always be 12 bytes, but depending upon the type specified in that particular entry, the values at the end could be of different types (or maybe just an address to another location in the file). What would be the best way to go about casting this sort of data? The smallest size memory I believe I would be dealing with would be 1 byte.

From stackoverflow
  • In C++ you should use a union.

    This is a mechanism by which you can define several, overlapping data types, possibly with a common header.

    See this article for how to use unions for exactly your problem -- a common header with different data underneath.

    Chad : Thanks, this seems exactly like what I was looking for.

0 comments:

Post a Comment