포인터 불리언 변환에 대해

DarkSide의 이미지

포인터 p에 대해,

if(p != nullptr)

if(p)

중 어느 것을 선호하시나요?
가독성으로 인해 전자를 쓴다는 의견이 다수이던데요.

shint의 이미지

http://codepad.org/3OSyqGCj

#include <stdio.h>
 
int main()
{
    char * p;
    p = NULL;
    if(p == NULL)
    {
    }
    return 0;
}

- C 에서 NULL 사용
- C++ 에서 nullptr 사용 (컴파일러 마다. 되거나 안되거나 다름)
- 스마트 포인터는 .use_count() 사용.

https://ideone.com/pjWBeS

#include <stdio.h>
#include <iostream>
 
using namespace std;
int main()
{
    int* n = NULL;
    printf("%x\n", &n);
    printf("%x\n", n);
 
    n = nullptr;
    printf("%x\n", &n);
    printf("%x\n", n);
    return 0;
}
 
//
ba1aa328
0
ba1aa328
0

//컴파일 오류 발생
http://codepad.org/HZRb8F4K

[C++11] 스마트포인터(Smart Pointer)
http://www.gisdeveloper.co.kr/?p=2094

//0x00 - C 언어
http://codepad.org/McRULz4O

#include <stdio.h>
 
int main()
{
    int* n = 0x00;
    printf("%x\n", &n);
    printf("%x\n", n);
 
    return 0;
}
 
//
dafb9a18
0
dafb9a18
0

//0x00 - C++ 컴파일 오류
http://codepad.org/fq8fiJmy

//
https://ideone.com/sGbwy7

#include <stdio.h>
#include <iostream>
 
using namespace std;
int main()
{
    int* n = 0x00;
    printf("%x\n", &n);
    printf("%x\n", n);
 
    n = nullptr;
    printf("%x\n", &n);
    printf("%x\n", n);
    return 0;
}
//
a7c33538
0
a7c33538
0

//----------------------------------------
//그 밖에 찾아본 내용도 파일로 첨부해봅니다.
//----------------------------------------
- Magic Number : 0xCCCCCCCC 등에 주소를 정해서 사용합니다.
- Exception Address
- Access Violation
- SEH (Structured Exception Handling)
- new 는 처리자 '라고 합니다.
- TSU Address 내용을 보면 .text1 .data1 .bss1 + .text2 .data2 .bss2 = merge output section -> executable image
메모리에 쌓이는게 아니라.
merge 된 정보가. 실행 주소에 매핑 됩니다.

댓글 첨부 파일: 

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

ifree의 이미지

예전 규격에서는 애매함을 피하기 위해 if (p != NULL) 로 쓰는 것이 권장되었지만,

현 규격에서는 null pointer 는 false 로, 그 외는 true 로 명시적으로 변환되도록 규정되었기 때문에 굳이 if (p != nullptr) 로 쓸 필요는 없습니다. 마치 if (a != false)와 같은 표현이죠.

더구나, 스마트포인터까지 들어가면, null pointer 와 비교하여 유효성을 판단하는 것이 무의미해지기 때문에, 이러한 표현은 과거의 관행이라고 생각합니다.

그냥 if (p) 로 쓰는게 가독성이 더 좋게 보이는데요.

DarkSide의 이미지

아직 신(?)문법에 익숙하지 않은 분들이 많아서 전자를 많이 쓰나 봅니다.

세벌의 이미지

저는 if(p)

nullptr 이 뭔가 찾아보니 C++에서만 쓰고 C에서는 안 쓰는 것 같네요.
if(p)
이런 표현을 C언어에서 자주 쓰기 때문에 이리 쓴다고 가독성이 떨어진다고 보긴 어렵겠네요.

댓글 달기

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