(void)0 가 의미하는 바는?

serphine의 이미지

소스에서 심심찮게 보이는데요..
대략 이런 형태로 쓰입니다.

#ifdef NDEBUG
#define assert(x) ((void)0)
#else
#define assert(x) ...

즉 디버그 모드가 아니면 assert 자리에 (void)0 이걸 찍는건데요..
아마 컴파일할 때 이부분은 그냥 지나가는 듯 합니다.

제가 궁금한건 (void)0 이것이 어떤 의미이기 때문에 그냥 지나가는 것인가요?
(void *)도 아니고, (void)이 어떤 경우에 쓰이나요?

ktd2004의 이미지

제가 (void)를 사용하는 경우를 말씀드리겠습니다.

int main(int argc, char *argv[])
{
    int unused_value;
    return 0;
}

위 소스를 gcc로 컴파일하면 결과는 다음과 같습니다.
[1004 0]# gcc --version
gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-27)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
root@ktdserver:~
[1005 0]# gcc -Wall -W a.c
a.c: In function 'main':
a.c:7: warning: unused variable 'unused_value'
a.c: At top level:
a.c:5: warning: unused parameter 'argc'
a.c:5: warning: unused parameter 'argv'

즉, unused_value, argc, argv를 사용하지 않는 변수라고 경고가 출력됩니다.
소스를 다음과 같이 고치면 어떤 경고도 출력되지 않습니다.
int main(int argc, char *argv[])
{
    int unused_value;
 
    (void)argc;
    (void)argv;
    (void)unused_value;
    return 0;
}
serphine의 이미지

(char) 캐스팅은 1바이트 정수형으로, (float) 캐스팅은 4바이트 실수형으로 바꾸어 주는 것과 같이,
(void) 캐스팅은 0바이트로 만들어서 메모리에서 읽을 수 없게 만들어버리는 것으로 생각하면 되는 건가요?

ktd2004의 이미지

이쯤되면 고수님들의 댓글이 달려야 하는데 이상하네요..
전 바닥이 드러나는 것 같습니다.

제가 알고있던 (void)의 의미는 해당 변수를 사용하지 않는다고,
명시적으로 해주는 역할로 알고 있습니다.

제가 학습이 깊지 못해서 이만...
고수님들의 더 자세한 답변을 기대하며...

coathanger의 이미지

아하 그렇군요..

cppig1995의 이미지

저 경우에는, 그냥 0이라면 컴파일러가 아무 의미 없다고 경고하겠죠?
그래서 void형으로 캐스팅하는 겁니다.



돼지군 작업실 rev. E: E-Prot (가명), Fourword 64bit OS, ...
Ubuntu Hardy Beta on I4 'jeongu' / 서명 변경일 2008/3/26

Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.

댓글 달기

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