assert() 함수 구현시 do while(0)을 사용하는 이유는?

taeyeung의 이미지

assert.h 안에서 assert를 구현할 때 보면은

#define assert(x)  do { \
                                   ..... ; \
                             } while(0)

이렇게 구현해 놓은 것을 볼 수 있는데요

#define assert(x) .....(x)

이렇게 하는 것이 더 간단한데 그렇게 하지 않고서 do while(0)

에 넣어서 구현하는 이유가 무엇인가요?

7339989b62a014c4ce6e31b3540bc7b5f06455024f22753f6235c935e8e5의 이미지

http://www.kernelnewbies.org/faq/ 에서 "Why do a lot of #defines in the kernel use do { ... } while(0)?" 부분을 살펴보세요.

taeyeung의 이미지

아하 그렇군요, 답변 감사합니다.

매크로 안에 정의 내용이 여러 문장이 되면

매크로가 사용되는 소스에서 조건이 이상하게 걸릴 수가 있어서

이를 방지하는 것이군요

1. 정의된 매크로

#define FOO(x) \
        printf("arg is %s\n", x); \
        do_something_useful(x);

2. 사용 예

if (blah == 2)
                FOO(blah);

3. 실제 내용

if (blah == 2)
                printf("arg is %s\n", blah);
                do_something_useful(blah);;

4. 수정된 매크로

#define FOO(x) do {\
        printf("arg is %s\n", x); \
        do_something_useful(x);
       while(0)

5. 수정된 매크로가 적용된 결과

if (blah == 2)
                do {
                        printf("arg is %s\n", blah);
                        do_something_useful(blah);
                } while (0);
kane의 이미지

내용을 옮기시면서 하나 빠뜨리셨기에..

블럭을 사용하기 위해서만이라면
do { } while(0) 대신 { } 를 사용하는 것도 가능하지만,
추가로 붙는 ';' 때문에
do { } while(0) 를 사용한다는 내용도 쓰여있군요.

if (...)
  MACRO(arg);
else
  statement;

에서

MARCO가 { }이면

if (...)
  { ... }
  ; /* empty statement */    <-- 요 부분 때문에 아래의 else에서 에러
else
  statement;

... 같은 문제가 발생할 수 있다는군요.
markboy의 이미지

예전에 비슷한 주제로 kldp에 글이 올라온 적이 있었습니다. (커널프로그래밍QnA에 올라왔었군요 :) )

http://bbs.kldp.org/viewtopic.php?t=45045

댓글 달기

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