0Pricing
CUDA Academy · บทเรียน

ค้นหาหน่วยความจำรั่วด้วย memcheck

การเข้าถึงนอกขอบเขตและตัวชี้อุปกรณ์ที่ไม่ถูกต้อง

ค้นหาหน่วยความจำรั่วด้วย memcheck เป็นบทเรียน CUDA Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน CUDA Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why memcheck Exists

GPU memory bugs rarely crash loudly, so compute-sanitizer with the memcheck tool watches every access and reports the trouble for you. 🛡️

The Modern Command

Run your program under the memcheck tool by prefixing it with compute-sanitizer, the modern replacement for the old cuda-memcheck.

compute-sanitizer --tool memcheck ./vecadd

Out-of-Bounds Reads

The classic bug memcheck catches is an out-of-bounds access, where a thread reads or writes past the end of a device array.

A Missing Bounds Check

Forgetting the guard lets extra threads run wild, so this index can stray beyond n and trigger an invalid global write.

int i = blockIdx.x * blockDim.x + threadIdx.x;
out[i] = a[i] + b[i];

Reading the Report

memcheck prints the error kind, the offending address, and the thread that caused it, pointing you straight at the bad access.

Get Line Numbers

Compile with -lineinfo so memcheck can map each error back to the exact source line instead of a raw address.

nvcc -lineinfo vecadd.cu -o vecadd

Detecting Leaks

Turn on the --leak-check option and memcheck reports any device memory you allocated but never released before exit.

compute-sanitizer --leak-check full ./vecadd

A Classic Leak

Allocating without a matching free leaks the buffer, and across many iterations that slowly exhausts device memory.

cudaMalloc(&d_a, bytes);
// ...used, but cudaFree(d_a) is missing

Invalid Device Pointers

memcheck also flags passing a host pointer where the kernel expects a device pointer, a mistake that silently corrupts results.

Misaligned Access

Some loads require aligned addresses, so memcheck warns about a misaligned access that would otherwise produce garbage or a fault.

Fix and Rerun

After each fix, run memcheck again until it reports zero errors. A clean run is your sign the memory bugs are gone.

Quick Check

What kind of bug is the memcheck tool primarily designed to find?

Recap

You ran memcheck, added -lineinfo for source lines, enabled leak checking, and learned its common error kinds. Clean runs mean safe memory. ✅

คำถามที่พบบ่อย

บทเรียน “ค้นหาหน่วยความจำรั่วด้วย memcheck” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ค้นหาหน่วยความจำรั่วด้วย memcheck” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส CUDA Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส CUDA Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ค้นหาหน่วยความจำรั่วด้วย memcheck”

การเข้าถึงนอกขอบเขตและตัวชี้อุปกรณ์ที่ไม่ถูกต้อง คุณปฏิบัติ CUDA Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน CUDA Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน CUDA Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ค้นหาหน่วยความจำรั่วด้วย memcheck” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน CUDA Academy นี้ได้ไหม

ได้ บทเรียน CUDA Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ก้าวผ่านเคอร์เนลด้วย cuda-gdb
  2. ค้นหาหน่วยความจำรั่วด้วย memcheck
  3. ตามหาการแข่งกันด้วย racecheck
  4. ตรวจจับข้อผิดพลาดการซิงโครไนซ์ด้วย synccheck
← กลับไปที่ CUDA Academy