ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I heard when define a struct, it is important to make the size of the struct memory aligned.
For example,
Code:
struct foo
{
char p;
char[3] align;
};
Code:
struct goo
{
char p;
};
struct foo is better than struct goo, even if we only use member p.
Allocate 3 bytes to align with memory. But I do not know why make memory aligned is better. Any reasons? (for example, prevent memory from running out?)
struct goo is better than struct foo. Leave padding and alignment issues to the compiler, it knows better than you what to do.
Anyway, the reason why aligned memory is sometimes better is because some processors are optimized for aligned access of words (it's the common case), and faster access for aligned bytes comes naturally from that.
In days gone by that was important, but I'm not so sure now.
The reason for such an approach in the structures was (and still is) that numeric calculations were performed faster if they started on a word boundary, hence padding was occasionally required. This goes back to my days of Fortran, Pascal and PL/1 where structures would declare individual bits, and so it was possible for an integer to be declared at a very inconvenient location.
Looking at C the language does the padding automatically for you. If you define a structure with a char, an int, a char and an int, use sizeof to see the size of that structure. Now define a structure with two intsfollowed by two chars and use sizeof again. The first structure will add padding so that the ints are on the proper boundaries, whilst the second doesn't need the padding.
Internal padding in the structure is platform dependent. Under ordinary circumstances you should not worry about alignment, since the compiler will handle it automatically.
However, if you have lot of elements in a structure definition and have the urge to save memory, then grouping like types together will usually reduce the amount of padding that occurs. (Do some diagnostics and printf the starting addresses of items in the structure to see if there are any improvements.)
There are various obscure reasons for it. The memory pipelines, internal and external caches and so-forth in a modern CPU are "wide," so they actually transfer a chunk of memory to or from the CPU each time. Compiler-writers are aware of the capabilities and the preferences of various types of hardware and they do a lot of things for you.
About the only place where you might need to give such things "conscious thought" is when you are matching your code to the data-format of some external data source or file. In that case, you might need to get very specific about exactly which byte goes where, whether they're "big-endian" or "little-endian," and more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.