Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should we not use #pragma pack?

Tags:

c

structure

In C, when we use structures, when would it be inappropriate to use #pragma pack directive..?

an addition to the question.....

Can someone please explain more on how might the accessing of unaligned data specially with a pointer fail?

like image 636
psaw.mora Avatar asked Oct 19 '11 14:10

psaw.mora


People also ask

When should apostrophes not be used?

Do not use an apostrophe in the possessive pronouns whose, ours, yours, his, hers, its, or theirs. Do not use an apostrophe in nouns that are plural but not possessive, such as CDs, 1000s, or 1960s. Do not use an apostrophe in verbs. Apostrophes sometimes show up in verbs that end in -s, such as marks, sees, or finds.

Why don't we use apostrophe?

Mistakingly adding an apostrophe in plural nouns is a common holiday grammar mistake, mostly because the rules for plurals, possessives and plural possessives are difficult to remember. But unless you're talking about a noun that belongs to another noun, there is no apostrophe in plural nouns.

When to use an apostrophe at the end of a word?

Use an apostrophe when showing possession If the plural of the word is formed by adding an "s" (for example, cats), place the apostrophe after the "s" (see guideline #3 below). If the plural of the word is formed without adding an "s" (for example, children), add apostrophe "s" ('s) as you would to the singular form.

Do I need an apostrophe?

Apostrophes are used for two main jobs, showing possession and showing omission . Apostrophes for possession show that a thing belongs to someone or something. For example Anna's book or the school's logo. Apostrophes for omission show where something, usually a letter, has been missed out to create a contraction .


2 Answers

Firmware developer here. #pragma pack is very familiar territory. I'll explain.

In general you should not use #pragma pack. Yes, it will make your structures smaller in memory since it eliminates all padding between struct members. But it can make accessing those members much more expensive since the members may no longer fall along their required alignment. For example, in ARM architectures, 4-byte ints are typically required to be 4-byte aligned, but in a packed struct they might not be. That means the compiler needs to add extra instructions to safely access that struct member, or the developer has to access it byte-by-byte and reconstruct the int manually. Either way it results in more code than an aligned access, so your struct ends up smaller but your accessing code potentially ends up slower and larger.

You should use #pragma pack when your structure must match an exact data layout. This typically happens when you are writing code to match a data transport or access specification... e.g., network protocols, storage protocols, device drivers that access HW registers. In those cases you may need #pragma pack to force your structures to match the spec-defined data layout. This will possibly incur the same performance penalty mentioned in the previous paragraph, but may be the only way to comply with the specification.

like image 189
Andrew Cottrell Avatar answered Oct 23 '22 12:10

Andrew Cottrell


I would say that you shouldn't pack unless there's a really good reason to do so.

When pack is specified, all the padding is stripped out. Therefore the struct members could be unaligned - which could have performance consequences.

like image 33
Mysticial Avatar answered Oct 23 '22 11:10

Mysticial