memset에서 packed struct 의 sizeof

elecneer의 이미지

ARM 컴파일러로 모바일 코딩을 하고 있습니다.

typedef PACKED struct _ContactsPSTData {
   boolean isUsed;
   uint16 index;
   uint16 iRecord;
   ...
   ...
} ContactsPSTData;


memset(&set_pbook_data[index], NULL, sizeof(ContactsPSTData));

위와 같이 __packed struct를 선언해서 사용할 때,
implicit cast of pointer loses '__packed' qualifier 와 같은 에러가
memset에서 sizeof할 때 발생하여 ARM 문서를 찾아보니 아래와 같은 설명이
되어있습니다.

Quote:

C2906E: implicit cast of pointer loses <qual> qualifier

For example: "implicit cast of pointer loses '__packed' qualifier"

The compiler has performed an implicit cast of a packed pointer,
and is reporting an error to avoid the __packed qualifier being cast away
unintentionally. The simplest solution for this is to make all casts of
packed pointers explicit in the source code.

explicit cast하는 해결책을 알고 싶습니다.

서지훈의 이미지

memset(&set_pbook_data[index], NULL, sizeof(struct _ContactsPSTData)); 뭐... 이정도면 되지 않을지요? 근데 PACKED형 조사 해보셨나요?

<어떠한 역경에도 굴하지 않는 '하양 지훈'>

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

익명 사용자의 이미지

Quote:
The compiler has performed an implicit cast of a packed pointer,
and is reporting an error to avoid the __packed qualifier being cast away
unintentionally. The simplest solution for this is to make all casts of
packed pointers explicit in the source code.

보통 컴파일러가 알아서대충(implicit) 캐스팅을 하는데,
사용자가 의도하지 않은 casting할까 겁나는데,
특히, ARM의 구조를 고려할때, __packed 로 선언된 것은
alignment와 관련있어서리, 즉, 1바이트->4바이트로 패딩하는...
그런데, 프로그래머가 이때 주의를 요한다는얘기인듯합니다.

만약, 어떤 구조체를 선언했는데, 그 크기가 10이라면, ARM에서는
4바이트단위 액세스규칙에 의해 문제가되고,
이를 위해 __packed 키워드를 사용하면, 컴파일러는 자동으로
패딩을 해서 12 즉, 4의배수크기로 메모리를 잡아주는데,
mem* 시리즈 , 여기서는 memset()등을 사용할때,

Quote:
$ man memset
...
SYNOPSIS
#include <string.h>

void *memset(void *s, int c, size_t n);
...

Quote:
memset(&set_pbook_data
    , NULL, sizeof(ContactsPSTData));

    첫번째 파라메터인 &set_pbook_data
      는 void *로
      캐스팅이 컴파일러가 알아서(implicit)하게 하게되는데,
      오류(프로그래머는 이를 원치 않았다면)를 방지하기 위해, 프로그래머가 명시적으로(explicit)하게 캐스팅하라는 얘기같습니다.

      다시말하자면, &set_pbook_data

        의 타입은
        ContactsPSTData *타입인데, 이를 컴파일러가 대충 알아서 void *로 캐스팅을 하게되는데.....
        이는 문제가 발생할 소지가 다분하기에, 프로그래머가 명시적으로 캐스팅해줘라..라는 얘기라는...
        ,,,,...
        아~ 말 안된다. :evil:
        ...
        요약하면,
        __packed로 선언된 임의의 타입에 대해, memset()등 포인터
        캐스팅을 사용하는 함수를 호출하는 경우 명시적으로 타입 캐스팅을 하자! 라는 얘기 같군요.
        실제로는,
        memset( (ContactsPSTData *) &set_pbook_data
          , NULL, sizeof(ContactsPSTData));

          모 이런식으로 하라는 얘기같습니다.

          * ARM을 구경도 못해봐서 맞는 얘긴지 모르겠군요. :oops:

          익명 사용자의 이미지

          덧붙여서 다음의 경우도


          __packed int *pi;
          memset( (void *) pi, 0, sizeof(__packed int));
          ..
          MyFunc((void *) pi);
          ..
          모 이런 식으로하라는 얘기 아닌지....

          elecneer의 이미지

          Quote:
          memset(&set_pbook_data
            , NULL, sizeof(struct _ContactsPSTData));

            Quote:
            memset( (ContactsPSTData *) &set_pbook_data
              , NULL, sizeof(ContactsPSTData));

              위와 같이도 해보고, explicit cast 하기위해 몇가지를 해보았지만 잘안되는군요. 워낙 지식이 짧아서..

              packed 에 대해서 알아보았지만, memset 에 대한 말은 없어서 질문 올렸었습니다.

              익명 사용자의 이미지

              질문이 있는데 원래 packed는

              struct foo {
                int x;
                char a, b, c, d;
              } __attribute__((packed));

              이런 식으로 선언하지 않나요?
              __packed는 처음 보는 듯....
              pynoos의 이미지

              http://cvs.sourceforge.net/viewcvs.py/madwifi/madwifi/hal/linux/ah_osdep.h?rev=1.4

              __packed 같은 것은 __attribute__((packed)) 로 재정의해서 사용합니다.
              다만 컴파일러에서 지원하는지여부에 따라 혹은 __attribute__ 표현이 너무 길어서 쓰이는 축약형일 뿐입니다.

              elecneer의 이미지

              Qualcomm 이 제공하는 DMSS 코드에는 아래와 같이 정의되어 있습니다.

              기존의 게시물도 그렇고 답변이 __packed 에 대한 말씀들이 많으신데,
              질문의 의도와는 약간 논점이 다른 것 같습니다.

              ARM 컴파일러의 모바일 코딩에서만의 문제인지는 모르겠지만,
              내공이 작아 어려움이 있네요..

              /* ---------------------------------------------------------------------
              ** Compiler Keyword Macros
              ** --------------------------------------------------------------------- */
              #if (! defined T_WINNT) && (! defined TARGET_OS_SOLARIS)
              #ifndef SWIG  /* The SWIG preprocessor gets confused by these */
                /* Non WinNT Targets
                */
                typedef  signed long long   int64;       /* Signed 64 bit value */
                typedef  unsigned long long uint64;      /* Unsigned 64 bit value */
                #define PACKED __packed
                #define INLINE __inline
                #define CDECL
              #endif /* SWIG */
                #define far
                #define near
                #define _far
                #define _near
              //  #define _cdecl
                #define cdecl
                #define _pascal
                #define _interrupt
              
              #else /* T_WINNT || TARGET_OS_SOLARIS */
              #error code not present
              #endif /* T_WINNT */
              

              댓글 달기

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