size를 정하지 않는 배열이 구조체에 있는 경우 어떻게 되는지 궁금합니다.

kgcrom의 이미지

안녕하세요.

요새 Redis 소스를 보고있습니다.
소스중에 string library라고 만들어놓은게 있어요.
그중에 궁금한 부분을 아래 코드로 만들어보았습니다.

struct sdshdr{
        int len;
        int free;
        char buf[];
};
int main()
{
        struct sdshdr sh;
        struct sdshdr *psh;
 
        printf("size of struct sdshdr : %d\n", sizeof(struct sdshdr));
 
        psh = malloc(sizeof(struct sdshdr) + 10 + 1);
 
        psh->len = 10;
        psh->free = 0;
 
        memcpy(psh->buf, "kgcrommass", 10);
        psh->buf[10] = '\0';
 
        printf("psh->buf : %s\n", psh->buf);
        return 0;
}

size를 지정하지 않은 배열은 선언과 동시에 초기화를 해야한다고 알고있었는데....sdshdr구조체에 있는게 궁금하구요.
더 궁금한건, sizeof(sdshdr)을 하게되면 8 값이 나와요.

마지막 부분에 psh->buf로 원하는 결과를 얻어 오는것도 의문이 가네요;;

pragma pack와 관련된건가요?

날씨가 많이 추워졌는데 감기조심하세요~~

매그넘의 이미지

메모리 할당을
구조체 사이즈 + 11byte 를 했습니다.

psh = malloc(sizeof(struct sdshdr) + 10 + 1);

buf 에 11byte 가 할당이 되었고
10byte 만큼의 문자열을 넣을 수 있겎네요.

구조체 멤버의 순서를 바꿔버리면 바로 깨져버리는 난감한 상황이 발생할 듯 해요.

매그넘의 이미지

저도 궁금해서 찾아봤는데 flexible array member 로 검색해보니 많이 자료가 나오네요 ^^

kgcrom의 이미지

으악~!!

이 키워드 바로 제가 찾던거에요. 감사합니다.....어떻게 찾으셨어요?;; 검색능력이 떨어지는듯....;;

"즉 배열이 구조체나 공용체의 마지막 요소인 경우에 한해 선언 시에 그 크기를 명시하지 않고
나중에 할당받을 수 있도록 배려한 것이다. "

buf[]가 마지막에 둔 이유가 있었군요....ㅎㅎ

흘러가고있는 지금 이 시간에 충실하자.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.