Skip to contentSkip to conversation

newby.-interpreting C

DISCUSSION soraberri 4 participants 6 messages Dec 20, 2004 – Dec 30, 2004

lore
Share & tools
lore

soraberri

author
#0 lore raw

Hi all,

Anyone could give me the meaning of this definition? Is the __attribute__ stuff what scares me. If you feel like I'm too desoriented, please tell me what should I know first.

typedef struct {
	uint8_t b[6];
} __attribute__((packed)) bdaddr_t;

thanks very much

#1 lore raw

this specific attributes is intended in order to avoid structure alignment in memory.
The compiler is performing alignment to any allocation to be a multiplication of 4k.
in order to disable this alignment __attribute__ packed is being used.

usually this is being used when working with networking:
if you have a structure of 5k and you will send it you will actually send 8k.

attributes information:
http://www.unixwiz.net/techtips/gnu-c-attributes.html

cheers

Quoted 17 lines · soraberri

Hi all,

Anyone could give me the meaning of this definition? Is the __attribute__ stuff what scares me. If you feel like I'm too desoriented, please tell me what should I know first.

typedef struct {
	uint8_t b[6];
} __attribute__((packed)) bdaddr_t;

thanks very much

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
In reply to soraberri

On Mon, 2004-12-20 20:07:54 +0100, soraberri <[email protected]> wrote in message <[email protected]>:

Quoted 9 lines

Hi all,

Anyone could give me the meaning of this definition? Is the __attribute__ stuff what scares me. If you feel like I'm too desoriented, please tell me what should I know first.

typedef struct {
	uint8_t b[6];
} __attribute__((packed)) bdaddr_t;

Compilers do use __attribute__ to allow programmers to modify the compiler's behavior in certain areas. "packed" tells the compiler to not start each variable at a natural alignment (ie. 4 bytes on a 32bit machine, 8 bytes on a 64bit machine). (That is, the address of a given variable must be a multiple of 4 (or 8) bytes.)

A "packed" structure is usually used for two things:

- In device drivers to fit a hardware device's memory-mapped register structure
- In poorly written programs to store variables into on-disk files to be read back later on by other programs.

General rule: if you don't *need* this for a good reason, or if you even don't know what it does, you don't need it, since it also introduces a performance penalty: CPUs tend to be fast at naturally aligned memory accesses, but quite slow on non-aligned accesses.

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
author
#3 lore raw
Quoted 25 lines · Jan-Benedict Glaw

On Mon, 2004-12-20 20:07:54 +0100, soraberri <[email protected]> wrote in message <[email protected]>:

Quoted 7 lines

Hi all,

Anyone could give me the meaning of this definition? Is the __attribute__ stuff what scares me. If you feel like I'm too desoriented, please tell me what should I know first.

typedef struct {
	uint8_t b[6];
} __attribute__((packed)) bdaddr_t;

Compilers do use __attribute__ to allow programmers to modify the compiler's behavior in certain areas. "packed" tells the compiler to not start each variable at a natural alignment (ie. 4 bytes on a 32bit machine, 8 bytes on a 64bit machine). (That is, the address of a given variable must be a multiple of 4 (or 8) bytes.)

A "packed" structure is usually used for two things:

- In device drivers to fit a hardware device's memory-mapped register structure
- In poorly written programs to store variables into on-disk files to be read back later on by other programs.

Quoted 7 lines

General rule: if you don't *need* this for a good reason, or if you even don't know what it does, you don't need it, since it also introduces a performance penalty: CPUs tend to be fast at naturally aligned memory accesses, but quite slow on non-aligned accesses.

MfG, JBG

All rigth, it has been very helpful, thank you very much

regards
Luis

#4 lore raw
In reply to Jan-Benedict Glaw
Quoted 4 lines · Jan-Benedict Glaw

General rule: if you don't *need* this for a good reason, or if you even don't know what it does, you don't need it, since it also introduces a performance penalty: CPUs tend to be fast at naturally aligned memory accesses, but quite slow on non-aligned accesses.

On most processor types, unaligned accesses will fail (either generate an exception or return bogus data). The x86 family is the exception, in that unaligned access merely reduces performance.

Signature · 1 line
Glynn Clements <[email protected]>
#5 lore raw

On Thu, 2004-12-30 11:26:08 +0000, Glynn Clements <[email protected]> wrote in message <[email protected]>:

Quoted 10 lines
Quoted 4 lines · Jan-Benedict Glaw

General rule: if you don't *need* this for a good reason, or if you even don't know what it does, you don't need it, since it also introduces a performance penalty: CPUs tend to be fast at naturally aligned memory accesses, but quite slow on non-aligned accesses.

On most processor types, unaligned accesses will fail (either generate an exception or return bogus data). The x86 family is the exception, in that unaligned access merely reduces performance.

As is the VAX :)

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));