What does #pragma pack(0) do
-
The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?
it resets the packing to the default size (actually, what it does depends on the compiler. some compilers don't support pack(0) at all, and some use it as a reset)
-
The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?
Here n has to be 1,2 4, 8 .... Now if don't specify any value or if the value is 0, then the members are packed to default packing size (which is 8 for many compilers). However few compilers will throw compilation error.
-
it resets the packing to the default size (actually, what it does depends on the compiler. some compilers don't support pack(0) at all, and some use it as a reset)
Nope. Default size is NOT stating a #pragma pack, which defaults to DWORD, ie 4 byte alignment in structures. #pragma pack 0 means there is no packing between data members, so they are contiguous in memory. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh!
============================== Nothing to say.
-
Here n has to be 1,2 4, 8 .... Now if don't specify any value or if the value is 0, then the members are packed to default packing size (which is 8 for many compilers). However few compilers will throw compilation error.
Nope. Default size is NOT stating a #pragma pack, which defaults to DWORD, ie 4 byte alignment in structures. #pragma pack 0 means there is no packing between data members, so they are contiguous in memory. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh! (Just checked my code. Its been a few months since I did any, I guess the old memory is fading....) :)
============================== Nothing to say.
-
The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?
#pragme pack 0 takes out all pading between data members in a struct so they are contiguous in memory. Very useful indeed since pretty much every data stream has no padding since it wastes bandwidth so being able to cast a chunk of memory to some zero packed struct gives you immediate and easy access to those data members. Consider an ethernet framed IP packet containing UDP and DHCP data. You can build a struct to grab the IP address requested directly from the data. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh!
============================== Nothing to say.
-
Nope. Default size is NOT stating a #pragma pack, which defaults to DWORD, ie 4 byte alignment in structures. #pragma pack 0 means there is no packing between data members, so they are contiguous in memory. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh! (Just checked my code. Its been a few months since I did any, I guess the old memory is fading....) :)
============================== Nothing to say.
#pragma pack(0)
#include
using namespace std;struct Test
{
char a;
int i;
};void main()
{
cout<I am using VS2008 SP1. The build is x86.
Now guess the result of sizeof(Test) ?? -
#pragma pack(0)
#include
using namespace std;struct Test
{
char a;
int i;
};void main()
{
cout<I am using VS2008 SP1. The build is x86.
Now guess the result of sizeof(Test) ?? -
Nope. Default size is NOT stating a #pragma pack, which defaults to DWORD, ie 4 byte alignment in structures. #pragma pack 0 means there is no packing between data members, so they are contiguous in memory. --edit-- Actually I am talking crap. pack (1) makes data member contiguous in memory, pack (0) resets packing. DOh!
============================== Nothing to say.
Erudite_Eric wrote:
pragma pack 0 means there is no packing between data members, so they are contiguous in memory.
no. that's pack(1) : align on single bytes. pack(n) specifies the structure alignment, not the number of bytes between structs.
-
I am reading 8 on my console.
-
The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?
-
The msdn says #pragma pack(n) will change current alignment value to n. But if n is zero, what will do?
-
#pragma pack(0)
#include
using namespace std;struct Test
{
char a;
int i;
};void main()
{
cout<I am using VS2008 SP1. The build is x86.
Now guess the result of sizeof(Test) ?? -
Why don't you continue reading
MSDN
?#pragma pack
documentation[^] states:Valid values are 1, 2, 4, 8, and 16.
Hence
0
is 'not valid' (I wouldn't try to make assumptions on a value marked as such).Veni, vidi, vici.
-
yu-jian wrote:
But if n is zero, what will do?
Add /WX to your compiler settings and recompile. You should pay more attention to compiler warnings. :) Best Wishes, -David Delaune
-
There is a error that Visual Studio 2008 only supports 1, 2, 4, 8... After add /WX to compiter.
See the documentation[^], which clearly states that the only valid values for
n
are 1, 2, 4, 8 and 16. Thus using 0 is an invalid#pragma
and will be ignored: the default packing (8) will be used.Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness
-
Erudite_Eric wrote:
pragma pack 0 means there is no packing between data members, so they are contiguous in memory.
no. that's pack(1) : align on single bytes. pack(n) specifies the structure alignment, not the number of bytes between structs.
-
I am reading 8 on my console.
-
See the documentation[^], which clearly states that the only valid values for
n
are 1, 2, 4, 8 and 16. Thus using 0 is an invalid#pragma
and will be ignored: the default packing (8) will be used.Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness