473,757 Members | 8,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

aligned / packed

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. :)
Jun 27 '08 #1
7 6876
On 2008-04-28 20:29, Travis wrote:
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.

--
Erik Wikström
Jun 27 '08 #2
Travis wrote:
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
Jun 27 '08 #3
Erik Wikström wrote:
On 2008-04-28 20:29, Travis wrote:
>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.
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.
--
Jim Langston
ta*******@rocke tmail.com
Jun 27 '08 #4
On Apr 28, 12:13*pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
Erik Wikström wrote:
On 2008-04-28 20:29, Travis wrote:
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.

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.

--
Jim Langston
tazmas...@rocke tmail.com
awesome thanks for the info. so used in low level stuff for the sake
of compactness and speed?

disclosure - it's an embedded device.
Jun 27 '08 #5
Travis wrote:
>
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.

--
Ian Collins.
Jun 27 '08 #6
On Apr 28, 2:29*pm, Travis <travis.bow...@ gmail.comwrote:
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/RealtimeMa...ndOrdering.htm

--
EventStudio 4.0 - http://www.Eventhelix.com/Eventstudio/
Sequence diagram based systems engineering tool
Jun 27 '08 #7
On May 1, 1:33 am, Ian Collins <ian-n...@hotmail.co mwrote:
Travis wrote:
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.
It may even increase the space. On many machines, accessing
misaligned data requires more instructions, so you increase the
code space.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8

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

Similar topics

56
10474
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
3
3850
by: Amaryllis | last post by:
Hi again, I'm new to the world of communicating between VB.NET and AS/400, so I've been posting a lot of questions lately since no one else in the company has done anything like this before. Hopefully, this one is fairly simple. I'm trying to insert records into a table on the 400. I can insert into a packed field that has no decimal places and into the alphanumeric fields as well. Everything seems to translate except the values that...
3
7379
by: Brian Henry | last post by:
Does anyone know of or know how to convert a COBOL packed decimal in a text file to a decimal that .NET can work with? we are importing Old COBOL data files that have packed data in them and need to convert it to a form we can use. thanks!
3
12146
by: parag.kanade | last post by:
I have a packet structure which has a field of Integer arrays, that is packed struct { int a; char b; int count; }foo; I need to initialize all the entries of count to a particular value,
4
5159
by: Spufi | last post by:
I have read several messages on converting packed decimals (EBCDIC) in a C++ program, but they were all for AS400 systems. I am looking for a way to convert the packed decimals in a COBOL program that is on a MVS (z/OS) system in a C++ program. Anyone have any idea how to do this? Is there a header or class that will do this?? Thanks in advanced!!
15
23982
by: Daniel Rudy | last post by:
What is the difference between packed and unpacked structs? -- Daniel Rudy Email address has been base64 encoded to reduce spam Decode email address using b64decode or uudecode -m Why geeks like computers: look chat date touch grep make unzip
9
3612
by: Spoon | last post by:
Hello everyone, As far as I understand, if I request a uint8_t buffer, it could be allocated anywhere. uint8_t *buf = new uint8_t By anywhere, I mean e.g. it could start at an odd address. Therefore it might be incorrect to access 32 bits at a time:
29
2827
by: K. Jennings | last post by:
I would like to get the result of malloc() such that it is aligned on a given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will u64 *p = malloc(N) ; do the trick in general? Here N is a positive integer, and I am assuming that malloc() succeeds in allocating the chunk of memory specified.
9
17632
by: Sikandar | last post by:
Hi, I am beginner in C. Pls let me know what is packed array in C. Where is it used? Thanks, Sikandar
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.