Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task: null과 nullptr #69

Closed
fkdl0048 opened this issue Oct 6, 2024 · 0 comments · Fixed by #77
Closed

Task: null과 nullptr #69

fkdl0048 opened this issue Oct 6, 2024 · 0 comments · Fixed by #77
Assignees
Labels

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Oct 6, 2024

null vs nullptr

C++에서 사용되는 null과 nullptr의 차이점을 알아보자. (사용 방법은 대략적으로 알고 있지만 실제 차이점을 명확히 모르는 경우)

null

null은 기존 C언어에서 사용된 관습적인 매크로로, 보통 #define NULL 0 또는 #define NULL ((void*)0)으로 정의된다. cstddef 헤더 파일에 정의되어 있으며, 0 또는 NULL로 사용된다.

배경과 기원이 이러해서 C++에서는 NULL이 단순히 정수 0으로 취급되며 포인터와 정수 간의 모호함을 유발한다. 예를 들어, NULLintchar* 모두에 대입할 수 있어서 컴파일러가 암묵적으로 형변환을 수행한다.

int* p = NULL; // int* p = 0;
char* q = NULL; // char* q = 0; => 컴파일러가 암묵적으로 형변환

이러한 코드는 코드 작성자에게 혼란을 줄 수 있다..! 실제 경험 따라서 C++11에서는 nullptr를 도입하여 이러한 문제를 해결하였다.

nullptr

C++11에서 등장한 nullptrnullptr_t 타입의 리터럴이다. nullptrnullptr_t 타입의 유일한 값이며, nullptr는 모든 포인터 타입으로 암묵적으로 변환되지 않는다. 따라서 위에서 말한 혼란을 방지할 수 있다.

void foo(int i);
void foo(char* p);

foo(nullptr); // foo(char* p); => nullptr는 모든 포인터 타입으로 암묵적으로 변환되지 않는다.

헷갈릴 수 있는 부분인 nullptrnullptr_t 타입의 리터럴이라는 용어로 이는 특별한 리터럴 상수로 nullptr이 메모리의 특정 위치를 차지하지 않으며 컴파일 타임에 처리되는 리터럴 상수라고 이해할 수 있다. nullptr_t가 타입이니 nullptr은 변수이고 전역적으로 사용되니 static인가? -> 이 흐름이 아닌 리터럴 상수라는 점을 기억하자.

@fkdl0048 fkdl0048 self-assigned this Oct 6, 2024
@fkdl0048 fkdl0048 added the C++ label Oct 6, 2024
@fkdl0048 fkdl0048 added this to Todo Oct 6, 2024
@fkdl0048 fkdl0048 moved this to In Progress in Todo Oct 6, 2024
@fkdl0048 fkdl0048 linked a pull request Oct 30, 2024 that will close this issue
@github-project-automation github-project-automation bot moved this from In Progress to Done in Todo Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant