473,386 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 15413
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.