Advertisement

byte alignment with VC

Started by April 03, 2005 08:58 AM
12 comments, last by traiger 19 years, 4 months ago
does anyone know where the byte alignment options are for MS VC++ version 7.0? The options I'm refering to are those that allow you to specify the number of bytes in an int etc. There may not be any but I'm sure I've seen them somewhere. Thanks
Found what I was looking for it was Project->PropertiesC/C++->Code Generation->Struct Member Alignment = 1 byte.

Not exactly what I originally asked for but it does make reading binary files easier.
Advertisement
You can't change the number of bytes an int type stores. I don't suppose you mean sizeof(int)?

If you are writing an entire object to the binary output stream, then I see why you would want 1 byte alignment. Although instead, simply rearrangement the order of how you list your member variables in the class definition might also result a more compact arrangement in memory, without having to move away from the default byte alignment.
I am reading from a ms3d file.
when you want to change the alignment do an #include with:
PshPack1.h, PshPack2.h, PshPack4.h, or PshPack8.h
depending of what alignment you want

when you are finished writing/reading the file, call an include with PopPack.h and it will go back to the previous alignment
thanks

alittle confused as to how to use that though.

#include <PshPack1.h>

void ReadFile(char *filename)
{

...

}

#include <PopPack.h>

Can't be right perhaps you could provide an example.
Advertisement
I agree with taby,
list your member variables from largest to smallest.
I'm reading someone elses file format (MS3D) so I really don't have much say over the order of the structures.
At work, we use the #pragma pack preprocessor reference to specify pack alignment in VC++. We use it when defining network/serial packets where alignment is important:

// push the current alignment#pragma pack(push)// align to 1 byte#pragma pack(1)// code...struct foo{/*.  .  insert useful code here  .  .*/};// go back to the original alignment#pragma pack(pop)


Hope that helps [smile]
you would of course end that with #pragma pack(pop) to restore the original alignment.
in gcc the same is achieved like this:

struct foo {
...
} __attribute((packed));

This topic is closed to new replies.

Advertisement