4

Is there a mechanism available in most "common" C/C++ compilers (for gcc/g++, compilers for common smart phones, Visual C++, etc...) to retrieve or generate data structure alignment information? (Note that this term encompasses alignment, padding, and arrangement.) I am looking to obtain this information without needing to run any C/C++ code on the target platform.

(E.g. Could such data be obtained through DWARF/DWARF2 reliably on compilers that support it?)

Update: Yes, one can use pahole with gcc. (I have to wonder if this would work with mingw32 and if so whether the '-g' flag used to output DWARF2 data modifies code generation.) Unfortunately, gcc isn't the only compiler I have to support. (Visual C++ is the most likely second candidate.)

Update 2: For Visual C++ this looks promising. I believe between Visual C++ and g++, Windows, Linux, Mac OS X, iOS, and Android NDK are covered.

Update 3: FYI, the main reason I have for obtaining such information is to avoid deserialization for high performance loading under CPU-bound conditions. If you can think of a better way of achieving this other than writing some custom tools that use data structure alignment information, please feel free to post. My vote finger is itchy.

Update 4 after a decade: A friend of mine created a C++ library using modern language features (C++20 and on) that helps with making zero-instruction serializable structures in a practically cross-platform way: https://github.com/KaylinFinke/data_serialization/tree/main. This would have solved my original need way back in the day, so I thought I'd let others know of it here.

Kaganar
  • 6,540
  • 2
  • 26
  • 59
  • 2
    Are you looking for `__alignof__` by chance? – user703016 May 30 '12 at 21:23
  • In short, no. Expanded the question to hopefully be clear. – Kaganar May 30 '12 at 21:25
  • 2
    If you are up for solutions bordering on hacks, you could use [offsetof](http://www.cplusplus.com/reference/clibrary/cstddef/offsetof/), and feed it to a [compile-time output solution](http://stackoverflow.com/q/5775790/335858) to produce member-by-member offsets and the overall `sizeof`. – Sergey Kalinichenko May 30 '12 at 21:45
  • I thought of doing this, but unfortunately that would make building extremely cumbersome as often the target platform is not convenient to run or obtain data from. – Kaganar May 30 '12 at 21:47
  • 1
    `-g` does not affect code generation at all, it causes the compiler to output additional linker sections that contain the debugging information. – caf May 30 '12 at 23:00
  • 2
    Why don't you just design your structures such that they won't have any padding? That way you also eliminate any issues about different ABIs on the same underlying platform (e.g. if one ABI has `long long` aligned at 8 bytes and another just at 4, it won't matter because you ensured that it's naturally on an 8-byte boundary anyway). – R.. GitHub STOP HELPING ICE May 31 '12 at 01:46
  • @R +1, forcing packing manually will work as well but is a bit trickier. – J.N. May 31 '12 at 02:08
  • @R, I've considered this as well. As stated, this is mainly a CPU-bound situation where I've got no cycles to spare. As a result, packing structures for performance is quite important. As a result, static optimization is worthwhile, but there's a certain threshold to the complexity I can withstand, hence my deliberation. – Kaganar May 31 '12 at 13:19
  • WTF does this mean, "the main reason I have for obtaining such information is to avoid deserialization for high performance loading under CPU-bound conditions. "? – Dan Jun 03 '12 at 21:11
  • @Dan: Ideally I'd just read blocks of data in a single asynchronous read and it'd already be structured properly for the program to use. No need to deserialize it into structures. I'm looking to waste little CPU time on loading. Since my application is CPU intensive, I want the structures to be conducive to be good performance, so I'd like the structures to be packed for performance. – Kaganar Jun 04 '12 at 16:23
  • 1
    it's a little MACRO-intensive, but you can use unnamed-unions to force the size of a field to, say, sizeof(int) on both CPUs: struct foo { union { char real_field; int real_fiels_pad; }; } – benzaita Jun 08 '12 at 11:17

1 Answers1

0

I'm not sure about other compilers, but in VC you can force struct member alignment with pragma. In fact, if you are writing an IPC mechanism that uses structs, you should force the alignment programatically around any declarations used by both processes. That way you can eliminate the potential for error if either application deviates from the standard struct member alignment.

#pragma pack(push, 1) // forces 1 byte alignment

typedef struct
{
    int sample;
    char sample2;
}FIVE_BYTES;

#pragma pack(pop) // returns to whatever alignment was in use prior to the push

See MSDN for more information

brad-tot
  • 783
  • 7
  • 9