0Pricing
CUDA Academy · Lesson

Guarding Against Out-of-Range

The if (i < n) bounds check.

Guarding Against Out-of-Range is a free CUDA Academy lesson on CoddyKit — lesson 2 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.

You Often Launch Too Many

Thread counts come in fixed block sizes, so you almost always launch a few extra threads beyond your array length. Those spares need handling.

What Goes Wrong

An extra thread computes an index past the end of the array. If it writes there, it touches memory it does not own, causing a silent out-of-bounds bug.

The GPU Will Not Warn You

Unlike a clean crash, an out-of-range write may corrupt nearby data or read garbage. The kernel keeps running, so the failure is invisible until results look wrong.

The Fix Is One Line

Before any thread uses its index, check that it falls inside the array. This tiny bounds check is the most important safety habit in CUDA.

if (i < n) {
    out[i] = a[i] + b[i];
}

Why Less Than, Not Less Or Equal

Valid indices run 0 to n minus 1. The strict i < n lets the last real element through and stops the first invalid one.

Idle Threads Just Exit

A thread whose index is out of range simply skips the work and returns. Doing nothing is perfectly safe and costs almost no time.

Pass n to the Kernel

The kernel cannot guess the array length, so you give it n as a parameter. Then the guard always knows where the data ends.

__global__ void add(float* a, float* b, float* out, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) out[i] = a[i] + b[i];
}

Guard Reads Too

It is not only writes. Reading a[i] past the end loads garbage or faults, so the same i < n check protects every access.

A Common Off-by-One

Writing i <= n by mistake lets one thread touch element n, which does not exist. Always keep the comparison strict.

Cheap Insurance

The branch costs almost nothing because extra threads are rare and exit fast. The safety it buys is worth far more than that tiny cost. ✅

Make It a Reflex

Treat the bounds check as part of the index formula itself. Compute i, then immediately guard it before doing anything else.

Quick Check

Pick the correct guard.

Recap

You learned to add if (i < n) after computing the index. This one line stops out-of-range reads and writes that the GPU would never warn you about. 🛡️

Frequently asked questions

Is the “Guarding Against Out-of-Range” lesson free?

Yes — the full text of “Guarding Against Out-of-Range” 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 “Guarding Against Out-of-Range”?

The if (i < n) bounds check. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Guarding Against Out-of-Range” 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

  1. The Classic Index Formula
  2. Guarding Against Out-of-Range
  3. Rounding Up the Block Count
  4. Grid-Stride Loops
← Back to CUDA Academy