cudaMalloc and cudaFree
Reserving and releasing global memory.
cudaMalloc and cudaFree is a free CUDA Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CUDA Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎉
Frequently asked questions
Is the “cudaMalloc and cudaFree” lesson free?
Yes — the full text of “cudaMalloc and cudaFree” is free to read here on the web, and the CUDA Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CUDA Academy course, upgrade to CoddyKit PRO.
What will I learn in “cudaMalloc and cudaFree”?
Reserving and releasing global memory. You practise CUDA Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CUDA Academy?
No prior experience is required. CUDA Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “cudaMalloc and cudaFree” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CUDA Academy lesson?
Yes. Every CUDA Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- cudaMalloc and cudaFree
- Pointers to GPU Memory
- cudaMemset for Initialization
- Avoiding Leaks and Double-Frees