aligned / packed

T

Travis

Hi all -

Most of my experience in c++ has been upper level. I am just now
getting into lower level stuff and I see this alot

class foo {
....
} __attribute__((aligned(1), packed));

I am having trouble finding any documentation online that explains
aligned and packed to an outsider or low level work so I thought I
would put it to this awesome community. :)
 
E

Erik Wikström

Hi all -

Most of my experience in c++ has been upper level. I am just now
getting into lower level stuff and I see this alot

class foo {
...
} __attribute__((aligned(1), packed));

I am having trouble finding any documentation online that explains
aligned and packed to an outsider or low level work so I thought I
would put it to this awesome community. :)

It is a gcc extension that allows you to control the layout (in memory)
of the class members, search the documentation at gcc.gnu.org for more
information.
 
R

red floyd

Travis said:
Hi all -

Most of my experience in c++ has been upper level. I am just now
getting into lower level stuff and I see this alot

class foo {
...
} __attribute__((aligned(1), packed));

I am having trouble finding any documentation online that explains
aligned and packed to an outsider or low level work so I thought I
would put it to this awesome community. :)

In addition to what Erik told you, you can also try asking in gnu.g++.help
 
J

Jim Langston

Erik said:
It is a gcc extension that allows you to control the layout (in
memory) of the class members, search the documentation at gcc.gnu.org
for more information.

In the Microsoft world it is something like:
#pragma packed

What it does is removes padding bytes inside of the data structures. For
instance, 4 byte integers want to start on a byte that is evenly divisible
by 4 (or 2 on some systems). So if you had the structure:

struct Foo
{
char x;
int y;
};

The size of this would not be 5 as may be expected, but 8. The reason is
there are 3 padding bytes added between x and y so that y starts on a 4 byte
boundary. Normally, this is not a problem, but sometimes it can be an
issue. For example, if someone were attempting to read a binary file into
this structure that had no padding bytes. It would not align up correctly.
Which is why compilers usually supply some way to remove padding bytes
inside of structures.
 
T

Travis

In the Microsoft world it is something like:
#pragma packed

What it does is removes padding bytes inside of the data structures.  For
instance, 4 byte integers want to start on a byte that is evenly divisible
by 4 (or 2 on some systems).  So if you had the structure:

struct Foo
{
   char x;
   int y;

};

The size of this would not be 5 as may be expected, but 8.  The reason is
there are 3 padding bytes added between x and y so that y starts on a 4 byte
boundary.  Normally, this is not a problem, but sometimes it can be an
issue.  For example, if someone were attempting to read a binary file into
this structure that had no padding bytes.  It would not align up correctly.
Which is why compilers usually supply some way to remove padding bytes
inside of structures.

awesome thanks for the info. so used in low level stuff for the sake
of compactness and speed?

disclosure - it's an embedded device.
 
I

Ian Collins

Travis said:
awesome thanks for the info. so used in low level stuff for the sake
of compactness and speed?

disclosure - it's an embedded device.

That depends on the platform, it may save space at the expense of
decreased performance due to misaligned accesses requiring multiple bus
cycles.
 
E

EventHelix.com

Hi all -

Most of my experience in c++ has been upper level. I am just now
getting into lower level stuff and I see this alot

class foo {
...

} __attribute__((aligned(1), packed));

I am having trouble finding any documentation online that explains
aligned and packed to an outsider or low level work so I thought I
would put it to this awesome community. :)

Refer to the following link for some background on byte alignment and
padding:

http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm
 
J

James Kanze

Travis wrote:
That depends on the platform, it may save space at the expense
of decreased performance due to misaligned accesses requiring
multiple bus cycles.

It may even increase the space. On many machines, accessing
misaligned data requires more instructions, so you increase the
code space.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top