Getrennte Adressräume
Warum ein Host-Pointer auf dem Device ungültig ist.
Getrennte Adressräume ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des CUDA Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Two Separate Memories
The CPU and GPU each own their own RAM. They do not share one address space, and that single fact shapes all of CUDA. 🧠
What an Address Means
A pointer is just a number naming a slot in memory. That slot only makes sense in the address space where it was created.
Host Pointers Are Local
A pointer from malloc names host memory. The GPU has no such slot, so handing it to a kernel points nowhere valid.
int* h = (int*)malloc(n * sizeof(int));Device Pointers Are Local Too
A pointer from cudaMalloc names device memory. The CPU cannot read it directly; dereferencing it on the host crashes.
int* d;
cudaMalloc(&d, n * sizeof(int));The Classic Mistake
Passing a host pointer into a kernel compiles fine but fails at runtime. The number is valid; the memory it names is not on the GPU.
Why It Compiles Anyway
Both pointers look like the same type to C++, so the compiler stays silent. The mismatch only bites when the GPU touches the address.
Bridging with cudaMemcpy
To move data between spaces you must copy it explicitly with cudaMemcpy. There is no automatic sharing across the divide.
cudaMemcpy(d, h, bytes, cudaMemcpyHostToDevice);A Naming Convention Helps
Many programmers prefix host pointers with h_ and device pointers with d_. It is a habit that prevents painful mix-ups.
float *h_a, *d_a;Kernels See Device Space
Inside a kernel every pointer you dereference must live in device memory. That is the only space the GPU threads can reach.
Unified Memory Hint
Later you will meet unified memory, one pointer valid on both sides. For now, keep host and device pointers strictly apart.
The Mental Model
Picture two rooms with no shared shelves. To use data in the other room you must carry a copy over, never just point across.
Quick Check
Let us confirm you understand separate address spaces.
Recap
You learned host and device live in separate address spaces, pointers are not interchangeable, and cudaMemcpy is how data crosses the gap. ✅
Häufig gestellte Fragen
Ist die Lektion „Getrennte Adressräume“ kostenlos?
Ja — der vollständige Text von „Getrennte Adressräume“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des CUDA Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der CUDA Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Getrennte Adressräume“?
Warum ein Host-Pointer auf dem Device ungültig ist. Du übst CUDA Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um CUDA Academy zu starten?
Keine Vorkenntnisse erforderlich. CUDA Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Getrennte Adressräume“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser CUDA Academy-Lektion Code schreiben und ausführen?
Ja. Jede CUDA Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Der Funktionsqualifizierer __global__
- __device__- und __host__-Funktionen
- Getrennte Adressräume
- Der Lebenszyklus eines CUDA-Programms