cudaMalloc und cudaFree
Reservieren und geben Sie globalen Speicher frei.
cudaMalloc und cudaFree ist eine kostenlose CUDA Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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.
The GPU Has Its Own RAM
Your GPU does not borrow the computer's main memory. It owns a separate pool called global memory that kernels read and write. 🧠
You Must Ask for It
Before a kernel can use device memory, you reserve a chunk yourself. The function that does this reserving is cudaMalloc.
The cudaMalloc Signature
cudaMalloc takes the address of a pointer and a byte count. It fills that pointer with a fresh device address.
float* d_data;
cudaMalloc(&d_data, n * sizeof(float));Why a Pointer to a Pointer
You pass &d_data so cudaMalloc can write the new address back into your variable. The return value is reserved for an error code.
Always Count in Bytes
cudaMalloc thinks in raw bytes, not elements. Multiply your element count by sizeof(type) so the buffer is the right size.
cudaMalloc(&d_arr, count * sizeof(int));Give It Back with cudaFree
When you finish with a buffer, return it to the GPU using cudaFree. This releases the global memory for other work.
cudaFree(d_data);Pairs, Like Lock and Key
Treat cudaMalloc and cudaFree as a pair. Every allocation you make should have exactly one matching free.
Memory Is Uninitialized
Fresh memory from cudaMalloc holds garbage values. Reading it before you write or copy data gives unpredictable results.
Allocation Can Fail
If the GPU is out of memory, cudaMalloc returns an error instead of crashing. Always check that return code in real code.
Global Memory Is Plentiful but Slow
Buffers from cudaMalloc live in global memory: gigabytes of space, yet the slowest tier on the chip. Use it generously but mind access patterns.
The Rhythm of Every Program
Nearly every CUDA program follows the same beat: cudaMalloc, copy data in, launch a kernel, copy results out, then cudaFree.
Quick Check
Let us confirm how cudaMalloc receives its target pointer.
Recap
You learned to reserve GPU memory with cudaMalloc, sized in bytes, and to release it with cudaFree in matched pairs. 🎉
Häufig gestellte Fragen
Ist die Lektion „cudaMalloc und cudaFree“ kostenlos?
Ja — der vollständige Text von „cudaMalloc und cudaFree“ 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 „cudaMalloc und cudaFree“?
Reservieren und geben Sie globalen Speicher frei. 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 1 von 4.
Wie lange dauert die Lektion „cudaMalloc und cudaFree“?
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
- cudaMalloc und cudaFree
- Pointer auf GPU-Speicher
- cudaMemset zur Initialisierung
- Leaks und Double-Frees vermeiden