__attribute__ ((packed)) 에 대해 질문입니다.

lovejin0309의 이미지

유영창님이 지은 "리눅스 디바이스 드라이버" 라는 책을 공부중입니다.

책 내용중에

typedef struct
{
    unsigned long size;
    unsigned char buff[128];
} __attribute__ ((packed)) ioctrl_test_info;

란 부분이 나옵니다.

__attribute__ ((packed)) 란 부분이 패딩 문제 때문에 해 주는 것으로 알고 있습니다.

그런데 "__attribute__ ((packed))" 를 붙여 주면 패딩을 안 해 준다는 것은 알겠는데, __attribute__ 나 ((packed)) 가 정확히 어떤 역활을 하기 때문에 패딩을 방지해 주는지를 명확히 모르겠습니다.

설명 부탁 드릴께요.

LinuxMC의 이미지

32비트 아키텍처인 경우
pad -> 1byte

ex)
struct {
char a;
int b;
char c;
short d;
}

|pad | pad | pad | a |
| b |
| d | pad | c |

packed

| d | c | a |
| b |

이렇게 pad 가 사라짐으로써 가용 메모리를 1 word 절약하였네요.

무제? 무죄!

익명 사용자의 이미지

감사합니다.

패딩에 대해선 알고 있구요.

__attribute__ 와 ((packed))에 대해서 알고 싶습니다.

그 지시자들이 정확히 어떤 뜻을 지니고 있는 걸까요?

익명 사용자의 이미지

Anonymous wrote:
감사합니다.

패딩에 대해선 알고 있구요.

__attribute__ 와 ((packed))에 대해서 알고 싶습니다.

그 지시자들이 정확히 어떤 뜻을 지니고 있는 걸까요?

__attribute__(...)의 인자로써 (packed) 속성이 지정된 겁니다.

정확한 뜻은 별 것 없이 '패딩을 하지 말아라'이죠.

익명 사용자의 이미지

컴파일 규격에 나오는 내용이며
컴파일 규격에 따라 내용이 조금씩 다르거나 사용하지 않는 것들이 있습니다.
C언어에서 예약어가 있는 것처럼
해당 구문도 예약어로써 컴파일 안에서 해당 문구를 해석해서 처리합니다.

익명 사용자의 이미지

굳이 packed를 할 필요가 없는 구조체인데..

구조체 요소를 보면 char배열이 long단위로 떨어지는 크기이므로 packed가 있으나 없으나 결과는 같습니다.

__attribute__ ((packed)) 는 빼도 될 것 같습니다.

sohn9086의 이미지

gcc가 제공하는 C extension기능 중, variable attribute라는 겁니다.

http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes

생산적인 댓글을 달자

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.