byte alignment with VC
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.
Not exactly what I originally asked for but it does make reading binary files easier.
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.
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.
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
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.
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.
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:
Hope that helps [smile]
// 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]
April 04, 2005 08:49 AM
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));
in gcc the same is achieved like this:
struct foo {
...
} __attribute((packed));
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement