473,513 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pragma pack(1) on struct contains object

In our project, one used the following struct

#pragma pack(1)
struct Foo {
uint8_t b1;
uint8_t b2;
uint8_t b3;
std::vector<uint8_t buf;
...
};
#pragma pack()

this work on PC, but not on ARM target using a gcc/g++ cross
compiler. The problem is, on ARM, after defined a variable of Foo
and want to access a method of buf, such as

Foo foo;
foo.buf.resize(100);

the program will exit. I guss it's a segment failure.
Can you experts explain for me the reason? I know, it is suspious
that why this guy use pack(1) on this kind of struct, but we can just
ignore it and focus on the underlying reason why the usage cause the
failure.

Thanks in advance.

-
narke

Jul 12 '08 #1
4 15428
On 12 Jul., 12:26, Steven Woody <narkewo...@gmail.comwrote:
In our project, one used the following struct

#pragma pack(1)
struct Foo {
* uint8_t * b1;
* uint8_t * b2;
* uint8_t * b3;
* std::vector<uint8_t*buf;
* ...};

#pragma pack()

this work on PC, but not on ARM target using a gcc/g++ cross
compiler. *The problem is, on ARM, after defined a variable of *Foo
and want to access a method of buf, such as

* * Foo foo;
* * foo.buf.resize(100);

the program will exit. *I guss it's a segment failure.

Can you experts explain for me the reason? *I know, it is *suspious
that why this guy use pack(1) on this kind of struct, but we can just
ignore it and focus on the underlying reason why the usage cause the
failure.
Because some processors require that certain types are properly
aligned. So stop packing your structures: even on x86 architectures,
packed structures (although allowed) cause the code to run much more
slowly.

/Peter
Jul 12 '08 #2
Steven Woody wrote:
In our project, one used the following struct

#pragma pack(1)
struct Foo {
uint8_t b1;
uint8_t b2;
uint8_t b3;
std::vector<uint8_t buf;
...
};
#pragma pack()

this work on PC, but not on ARM target using a gcc/g++ cross
compiler. The problem is, on ARM, after defined a variable of Foo
and want to access a method of buf, such as

Foo foo;
foo.buf.resize(100);

the program will exit. I guss it's a segment failure.
Can you experts explain for me the reason?
Because you enforce mis-alingment. And while PCs can still access
mis-algined data (though at reduced speed), an ARM CPU simply can't.
I know, it is suspious that why this guy use pack(1) on this kind of
struct, but we can just ignore it and focus on the underlying reason why
the usage cause the failure.
Well that reason is at the same time the reason why this looks suspicious,
since it's also the reason why you shouldn't do that in the first place.
Jul 12 '08 #3
On Jul 13, 1:25 am, Rolf Magnus <ramag...@t-online.dewrote:
Steven Woody wrote:
In our project, one used the following struct
#pragma pack(1)
struct Foo {
uint8_t b1;
uint8_t b2;
uint8_t b3;
std::vector<uint8_t buf;
...
};
#pragma pack()
this work on PC, but not on ARM target using a gcc/g++ cross
compiler. The problem is, on ARM, after defined a variable of Foo
and want to access a method of buf, such as
Foo foo;
foo.buf.resize(100);
the program will exit. I guss it's a segment failure.
Can you experts explain for me the reason?

Because you enforce mis-alingment. And while PCs can still access
mis-algined data (though at reduced speed), an ARM CPU simply can't.
I know, it is suspious that why this guy use pack(1) on this kind of
struct, but we can just ignore it and focus on the underlying reason why
the usage cause the failure.

Well that reason is at the same time the reason why this looks suspicious,
since it's also the reason why you shouldn't do that in the first place.

Could you explain? Thanks.

And, I am thinking another question: whether using packing a struct is
itself a bad notion at all. You know, we are writting a network
program, other members in the team tends to define a structure for
every kind of packet come in/out from/to the network, because there is
no padding in packets in the form of byte-stream, so "#pragma pack(1)"
or "__attribute__((packed))" was used almost everywhere. I personally
dont' prefer this method, I just do the packet parsing and framing in
a character by character way, hence don't mapping packets to any in-
memory structure. Do these two kind of programming style imply
anything? What kind do you prefer?

Thanks in advance.
Jul 13 '08 #4
Steven Woody wrote:
>
*Please* don't quote sigs
>

In your previous post, I thing I don't fully understand the term 'mis-
alignment':
Most, if not all processors prefer types to be aligned to their natural
boundary, say 4 bytes for an int. When an int value is not aligned on a
4 byte boundary, the processor may require one or two extra bus cycles
to read the value form memory.

Some processors (Sparc for example) *require* types to be aligned to
their natural boundary. They will generate a hardware trap it you
attempt a misaligned access.

--
Ian Collins.
Jul 13 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1834
by: codymanix | last post by:
i want to pass a struct by reference to a function in a c++ dll. the problem is that the call causes an ArgumentException in the calling c# code. i already tried to use "out" instead of "ref"...
11
2265
by: James Gregory | last post by:
I'm pretty sure this is non-totally-trivial enough and standard-C++ enough to post here, however much it may initially look like a "how do I make a computer game?" question, though I may be wrong....
9
2371
by: Giovanni Bajo | last post by:
Hello, with Visual Studio .NET 2003: __declspec(align(1)) struct X { unsigned int x : 3; unsigned int y : 13; };
1
1532
by: Mike Margerum | last post by:
I seem to be running into an issue where I am deriving a new class from a struct with a different alignemnt via the pragma pack #pragma pack(1) typedef struct { unsigned short a; unsigned...
3
1460
by: GeLLcheN | last post by:
Hello everybody, I've got a little (endian) problem. I'm programming a network-based application, in which I'll use structs, interpreting ethernet packets. My intention is to read the stream...
6
26698
by: Jack | last post by:
Hi, All, is it possible to send a struct using the the send function. Here is what I mean typedef struct{ int ID; char name; }sampleStruct; int main(){
3
4747
by: Jimmy | last post by:
Struct definition as following(on 32-bit Linux): #pragma pack(push, 8) struct MY_STRUCT { char a; short b; short c; short d; int e;
6
2399
by: Boltar | last post by:
If I use #pragma pack to byte align structures to non word size , will this cause the program to run slightly slower if it is accessing and setting non word aligned integers etc in the struct? Is...
2
2746
by: Henry Yiu | last post by:
In the following structure, bytesPerSector is not aligned to the sizeof(short), so after I read the raw data into buffer, and try to extract the bytesPerSector portion, I got the wrong number. Is...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.