/

 

|

C++ 하드웨어 매핑(Mapping to Hardware)의 핵심은 기본 연산이 단일 기계 명령(machine instruction)으로 직접 변환되어 추가적인 런타임 오버헤드 없이 하드웨어 자원을 활용한다는 것입니다. 대입 연산은 메모리 칸의 값을 복사하며, 포인터는 주소를 복사하여 가리키는 대상을 바꾸는 반면, 참조(Reference)는 대상 객체의 값을 직접 바꿉니다. 이 글에서는 A Tour of C++ 1.9를 바탕으로 C++의 메모리 모델과 포인터, 참조의 차이를 설명합니다.

C++ 코드는 어떻게 하드웨어와 메모리에 매핑됩니까?

C++의 기본 연산 대부분은 특정 CPU 명령 하나에 직접 대응하도록 설계되어 있습니다. 예를 들어 int 값의 덧셈은 대부분의 아키텍처에서 정수 덧셈 명령 하나로 컴파일됩니다. 이런 저수준 대응 관계 때문에 C++은 추가적인 런타임 오버헤드 없이 하드웨어 자원을 거의 그대로 활용할 수 있습니다.

메모리는 연속된 칸(cell)들이 나열된 구조로 볼 수 있습니다. 포인터에 저장되는 값은 바로 이 칸들 중 하나를 가리키는 기계 주소입니다. 배열은 이런 메모리 모델 위에서 서로 붙어 있는 객체들의 연속을 추상화한 것이며, 그래서 배열의 첫 원소 주소만 알면 나머지 원소들의 위치도 계산할 수 있습니다.

여기서 가장 먼저 짚어야 할 규칙은 대입은 값을 복사한다는 점입니다. x = y;를 실행하면 y가 가진 값이 x의 메모리 칸에 복사될 뿐, 두 변수가 같은 칸을 공유하게 되는 것은 아닙니다. 따라서 이후 어느 한쪽을 바꿔도 다른 쪽에는 영향이 없습니다. 두 변수가 진짜로 상태를 공유해야 한다면, 포인터나 참조를 통해 그 관계를 명시적으로 만들어야 합니다.

C++에서 대입(Assignment)과 초기화(Initialization)는 어떻게 다릅니까?

대입과 초기화는 둘 다 = 기호를 쓰지만 메모리 관점에서는 서로 다른 작업입니다. 초기화(initialization)는 아직 유효한 값을 갖지 않은 메모리 칸을 유효한 객체로 만드는 과정이고, 대입(assignment)은 이미 유효한 객체로 존재하는 메모리에 새로운 값을 덮어쓰는 작업입니다.

int x = 1; // 초기화: 새 객체 x를 값 1로 만듭니다.
int y = 3; // 초기화: 새 객체 y를 값 3으로 만듭니다.

x = y;     // 대입: y의 값(3)을 x가 가진 칸에 복사합니다. (x == 3)
y = 100;   // 대입: y만 바뀝니다. x는 여전히 3입니다.

x = y; 이후 x와 y는 값이 같아 보일 뿐, 서로 독립된 메모리 칸입니다. 그래서 y = 100;을 실행해도 x는 영향을 받지 않고 3에 그대로 머무릅니다. 이 독립성이 대입 연산의 기본 성질이며, 뒤에서 다룰 포인터·참조와 대비되는 지점입니다.

C++ 포인터(Pointer)와 참조(Reference)의 동작 차이는 무엇입니까?

포인터와 참조는 둘 다 다른 객체를 가리킨다는 점에서 비슷해 보이지만, 대입이 일어날 때 실제로 바뀌는 대상이 서로 다릅니다.

int x = 2;
int y = 3;

int* p = &x;  // p는 x의 주소를 저장합니다.
int* q = &y;  // q는 y의 주소를 저장합니다.

int& r  = x;  // r은 x에 묶인 참조(alias)입니다.
int& r2 = y;  // r2는 y에 묶인 참조(alias)입니다.

p = q;   // 포인터 대입: 주소가 복사됩니다. 이제 p도 y를 가리킵니다. x는 여전히 2입니다.
r = r2;  // 참조 대입: r이 가리키는 대상(x)에 r2의 값(3)이 쓰입니다. x는 3이 됩니다.

포인터 대입(p = q)은 주소 값 자체를 복사하는 연산입니다. 그 결과 p가 가리키는 대상이 x에서 y로 바뀌지만, x가 들어 있던 메모리의 값은 전혀 건드리지 않습니다. 반면 참조 대입(r = r2)은 참조 자체를 재지정하는 문법이 없기 때문에, =가 항상 참조가 가리키는 대상 객체에 대한 값 대입으로 해석됩니다. 그래서 r = r2;는 r2가 가리키는 값(3)을 r이 가리키는 객체, 즉 x에 복사하는 것과 같습니다.

이 차이는 참조가 만들어지는 방식과도 이어집니다. int& r = x;에서 =는 대입이 아니라 r을 x에 묶는 초기화입니다. 참조는 반드시 어떤 객체에 묶인 상태로만 존재할 수 있어서, 초기화 없이 참조만 선언하는 것은 허용되지 않습니다. 포인터는 nullptr로 초기화하거나 나중에 다른 주소로 재대입할 수 있지만, 참조는 한 번 묶인 대상을 끝까지 바꿀 수 없습니다.

구분 포인터 대입 p = q 참조 대입 r = r2
복사되는 것 주소(가리키는 대상) 값(대상 객체의 내용)
바뀌는 대상 포인터 자신이 가리키는 곳 참조가 이미 가리키던 객체의 값
재지정 가능 여부 가능 (다른 주소로 대입 가능) 불가능 (초기화 시점에 묶인 대상 고정)

학습 내용 요약 및 다음 단계는 무엇입니까?

지금까지 정리한 내용을 위 코드 예시에 맞춰 다시 확인해 보면 다음과 같습니다.

  • x = y; 이후 y = 100;을 실행해도 x는 3으로 그대로 남습니다. 대입은 값 복사이므로 두 변수는 독립적입니다.
  • p = q; 이후 x는 여전히 2이고, p와 q 둘 다 y를 가리키게 됩니다. 포인터 대입은 주소 복사입니다.
  • r = r2; 이후 x는 3이 되고, r은 변함없이 x를 가리킵니다. 참조 대입은 대상 객체에 대한 값 대입입니다.

정리하면, 포인터 대입은 “무엇을 가리키는지”를 바꾸고, 참조 대입은 “가리키고 있는 대상의 값”을 바꿉니다. 이 구분을 명확히 해두면 이후 함수 인자 전달이나 자료구조 구현에서 포인터·참조를 고를 때 헷갈리지 않습니다. 1장의 다음, 그리고 마지막 절은 1.10 Advice이며, 다음 글에서 이어서 정리할 예정입니다.

정보 출처는 어디입니까?

본문 내용은 2026-09-15 기준으로 확인한 Bjarne Stroustrup의 저서와 공식 문서를 참고했습니다.

  • Bjarne Stroustrup, A Tour of C++ (3rd ed.), §1.9 Mapping to Hardware — 이 글의 주요 학습 출처입니다.
  • Memory model — cppreference.com — 메모리를 연속된 칸으로 보는 모델과 포인터가 저장하는 주소의 의미를 확인했습니다.
  • References — Standard C++ Foundation FAQ — 참조가 초기화 시점에 대상에 묶이고 이후 재지정할 수 없다는 규칙을 확인했습니다.

이 글은 Bjarne Stroustrup, A Tour of C++ 3rd ed. §1.9 Mapping to Hardware 공부 노트를 2026-09-15 기준으로 정리한 일반 설명이며, 표준·구현·개정판에 따라 세부 서술은 달라질 수 있습니다.

The core of C++ Mapping to Hardware is that basic operations translate directly into single machine instructions, utilizing hardware resources without extra runtime overhead. Assignment operations copy values between memory cells, while pointers copy addresses to change what they point to. References, however, directly change the value of the object they refer to. This article explains the C++ memory model and the differences between pointers and references based on A Tour of C++ 1.9.

How Does C++ Code Map to Hardware and Memory?

Most of C++'s basic operations are designed to translate into a single machine instruction. Adding two int values, for instance, typically compiles down to one integer-add instruction on most architectures. This tight correspondence is why C++ can use hardware resources almost directly, without extra runtime overhead layered on top.

Memory itself can be pictured as a sequence of contiguous cells. A pointer's value is nothing more than the machine address of one such cell. An array builds on this same model: it's an abstraction over a run of objects laid out back-to-back, which is exactly why knowing the address of the first element is enough to compute the location of every other element.

The rule worth internalizing first is that ordinary assignment copies a value. Executing x = y; copies whatever value y holds into x's own memory cell — it does not make the two variables share a cell. Changing one afterward has no effect on the other. If two variables genuinely need to share state, that relationship has to be created explicitly, through a pointer or a reference.

What is the Difference Between Initialization and Assignment in C++?

Initialization and assignment both use the = symbol, but they do very different things in terms of memory. Initialization takes a memory cell that doesn't yet hold a valid value and turns it into a proper object. Assignment takes a cell that already holds a valid object and overwrites its value.

int x = 1; // initialization: creates x with value 1
int y = 3; // initialization: creates y with value 3

x = y;     // assignment: copies y's value (3) into x's cell (x == 3)
y = 100;   // assignment: only y changes; x is still 3

After x = y;, x and y happen to hold the same value, but they remain two separate cells in memory. That's why y = 100; leaves x untouched at 3. This independence is the defining property of plain assignment — and it's exactly what pointers and references break out of, as the next section shows.

How Do Pointers and References Differ in C++?

Pointers and references can look similar because both let you "reach" another object, but assignment through each one touches a different thing under the hood.

int x = 2;
int y = 3;

int* p = &x;  // p stores the address of x
int* q = &y;  // q stores the address of y

int& r  = x;  // r is bound to x — an alias, not a copy
int& r2 = y;  // r2 is bound to y

p = q;   // pointer assignment: the address is copied; p now points to y. x is still 2
r = r2;  // reference assignment: r2's value (3) is written into whatever r refers to (x). x becomes 3

Pointer assignment (p = q) copies the address value itself. As a result, p now points at y instead of x, but the memory that held x is never touched. Reference assignment (r = r2) works completely differently, because C++ has no syntax to "re-point" an existing reference — = on a reference is always interpreted as a value assignment to whatever object it already refers to. So r = r2; reads the value r2 refers to (3) and writes it into the object r refers to, which is x.

This difference traces straight back to how references come into existence. In int& r = x;, that = is not an assignment at all — it's the initialization that binds r to x. A reference can only exist bound to an object, so declaring one without initializing it is not legal. A pointer can be initialized to nullptr or later reassigned to a different address, but once a reference is bound, it can never be made to refer to anything else.

  Pointer assignment p = q Reference assignment r = r2
What gets copied The address (what is pointed to) The value (the referred-to object's content)
What actually changes Where the pointer itself points The value of the object the reference already refers to
Can it be re-bound? Yes — can be reassigned to a new address No — bound permanently at initialization

What Are the Summary and Next Steps?

Running the checks against the code above confirms the distinctions made in this article.

  • After x = y;, executing y = 100; leaves x at 3. Assignment copies values, so the two variables stay independent.
  • After p = q;, x is still 2, and both p and q now point to y. Pointer assignment copies an address.
  • After r = r2;, x becomes 3, while r still refers to x exactly as before. Reference assignment is a value write to the referred-to object.

The short version: pointer assignment changes what is being pointed to, while reference assignment changes the value of what is already being referred to. Keeping that distinction clear removes a lot of confusion later, when choosing between pointers and references for function parameters or data structures. The next — and final — section of Chapter 1 is 1.10 Advice, covered in the next article in this series.

What Are the Sources?

The contents are based on Bjarne Stroustrup's writings and official documentation as of 2026-09-15.

  • Bjarne Stroustrup, A Tour of C++ (3rd ed.), §1.9 Mapping to Hardware — The main study source for this article.
  • Memory model — cppreference.com — Confirmed the model of memory as contiguous cells and the meaning of addresses in pointers.
  • References — Standard C++ Foundation FAQ — Confirmed the rule that references are bound at initialization and cannot be reseated.

This article is a general explanation based on study notes from Bjarne Stroustrup, A Tour of C++ 3rd ed. §1.9 Mapping to Hardware as of 2026-09-15. Details may vary by standard, implementation, or edition.

+ Recent posts