매크로 함수 관련 질문

ppappa의 이미지

typedef struct test_timer test_timer;
struct test_timer
{
   void *cookie;
   int a;
   int b;
};

#define pTimer(t) ( (test_timer *)(t) )

이 define 부분이 이해가 잘 안가네요...
이 매크로 함수를 사용할 때

unsinged long t = some_func();
test_timer *tt = pTimer(t);

이 문장이 어떻게 성립하죠?
cdpark의 이미지

long과 pointer의 크기가 모두 32bit라고 가정하면 그런대로 동작합니다.

대부분의 32비트 시스템에서는 동작하겠지만, 포팅할 때에 문제가 될 듯 싶네요. (64비트라던가, long과 pointer의 크기가 다른 시스템이라던가..)

some_func()의 내부를 들여다보면 (test_timer *)를 long으로 바꿔 돌려주는 부분이 있을지 모르겠네요. 확인해보세요.

yeppiguy의 이미지

Quote:
typedef struct test_timer test_timer;
struct test_timer
{
void *cookie;
int a;
int b;
};

#define pTimer(t) ( (test_timer *)(t) )


define문은 단순히 타입캐스킹하는 매크로입니다. 즉, 주어진 t인자를
test_timer 스트럭쳐 포인터 형으로 캐스팅하는거죠...

Quote:
unsinged long t = some_func();
test_timer *tt = pTimer(t);

위에서는 some_func에 의해서 t값을 test_timer스트럭쳐 포인터 형인 tt에 assign하는 건데, 몬가 좀 이상하네요... some_func이 몰까요?...ㅎㅎ
ppappa의 이미지

제가 매크로 함수 개념이 없어서 그러는데요...

#define pTimer(t) ( (test_timer *)(t) )

이 부분이
t 를 test_timer * 형으로 형변환하는 것 같은데요

unsigned long t = some_func();
test_timer *tt = pTimer(t);
에서
unsinged long 형을 test_timer * 형으로 형변환이 가능하단 말인가요?어떻게 이게 가능한가요???
unsigned long이 32비트이고 test_timer *가 구조체긴 하지만 포인터 변수이기 때문에 같은 32비트라서 가능한건가요?
이해가 잘.........
^^
doldori의 이미지

perihk wrote:
unsigned long이 32비트이고 test_timer *가 구조체긴 하지만 포인터 변수이기 때문에 같은 32비트라서 가능한건가요?

네, 그렇습니다. 캐스팅의 위력은 실로 엄청나죠. :)

잘 쓰면 무척 편리하지만 잘못 쓰면 치명적인 결과를 초래할 수도 있습니다.

그리고 이 코드는 특정 환경에서만 작동할 뿐 이식성은 없다는 점도 잊지 마세요.

댓글 달기

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