常见错误与调试

Tutorial: 教程一 Category: C语言 Published: 2026-04-07 13:58:26 Views: 20 Likes: 0 Comments: 0
12. 常见错误与调试

在软件开发过程中,错误是不可避免的。理解和识别常见的错误类型,以及掌握有效的调试技巧,是编写高质量、可靠 C 程序的关键。本章将详细探讨 C 语言中的常见编译错误和运行时错误,并介绍几种实用的调试技巧和内存泄漏检测工具,帮助你迅速定位和修复程序中的问题。

12.1 常见编译错误

编译错误是在编译过程中由编译器检测到的问题,这些错误阻止程序成功编译。理解常见的编译错误类型及其原因,有助于快速修复代码中的问题。

12.1.1 语法错误(Syntax Errors)

定义:语法错误是指代码不符合 C 语言的语法规则,导致编译器无法理解代码的含义。

常见原因

  • 缺少分号(;
  • 括号不匹配
  • 拼写错误
  • 错误的关键字使用

示例

#include <stdio.h>

int main() {
    printf("Hello, World!") // 缺少分号
    return 0;
}

编译器输出

error: expected ';' before 'return'

修正后的代码

#include <stdio.h>

int main() {
    printf("Hello, World!"); // 添加分号
    return 0;
}
12.1.2 类型错误(Type Errors)

定义:类型错误是指变量或表达式的类型与预期不符,导致编译器无法正确处理。

常见原因

  • 将不同类型的变量进行不兼容的操作
  • 函数返回类型与声明不一致
  • 变量未正确初始化

示例

#include <stdio.h>

// 函数声明返回int类型
int add(int a, int b);

int main() {
    float result = add(5, 10); // 尝试将int返回值赋给float变量
    printf("Result: %.2f\n", result);
    return 0;
}

// 函数定义返回int类型
int add(int a, int b) {
    return a + b;
}

编译器输出(在某些编译器中可能不会报错,但可能导致数据精度丢失):

warning: implicit conversion from 'int' to 'float' changes value from '15' to '15.000000' [-Wint-to-float-conversion]

修正后的代码

#include <stdio.h>

// 函数声明返回float类型
float add(int a, int b);

int main() {
    float result = add(5, 10); // 正确匹配类型
    printf("Result: %.2f\n", result);
    return 0;
}

// 函数定义返回float类型
float add(int a, int b) {
    return (float)(a + b);
}
12.1.3 未定义的变量或函数

定义:未定义的变量或函数是指在使用之前没有声明或定义的变量或函数。

常见原因

  • 变量未声明
  • 函数未声明或定义
  • 错误的作用域

示例

#include <stdio.h>

int main() {
    printf("The value of x is %d\n", x); // 使用未定义的变量x
    return 0;
}

编译器输出

error: 'x' undeclared (first use in this function)

修正后的代码

#include <stdio.h>

int main() {
    int x = 10; // 声明并初始化变量x
    printf("The value of x is %d\n", x);
    return 0;
}
12.1.4 括号不匹配

定义:括号不匹配是指在代码中使用的括号({}, (), [])数量不对或位置错误,导致编译器无法正确解析代码结构。

示例

#include <stdio.h>

int main() {
    if (1) {
        printf("Hello, World!\n";
    }
    return 0;
}

编译器输出

error: expected ')' before ';' token

修正后的代码

#include <stdio.h>

int main() {
    if (1) {
        printf("Hello, World!\n"); // 添加右括号
    }
    return 0;
}
12.2 运行时错误

运行时错误是在程序编译成功后,在程序运行过程中发生的错误。这些错误通常导致程序异常终止或产生不正确的结果。

12.2.1 除以零错误

定义:当程序尝试将一个数除以零时,会导致除以零错误,通常会引发程序崩溃。

示例

#include <stdio.h>

int main() {
    int a = 10;
    int b = 0;
    int c = a / b; // 除以零
    printf("Result: %d\n", c);
    return 0;
}

运行时输出

Floating point exception (core dumped)

修正后的代码

#include <stdio.h>

int main() {
    int a = 10;
    int b = 0;

    if(b != 0) {
        int c = a / b;
        printf("Result: %d\n", c);
    } else {
        printf("Error: Division by zero is not allowed.\n");
    }

    return 0;
}

运行时输出

Error: Division by zero is not allowed.
12.2.2 空指针引用

定义:空指针引用是指程序尝试访问或修改一个未初始化或已释放的指针所指向的内存位置。

示例

#include <stdio.h>

int main() {
    int *ptr = NULL;
    printf("Value: %d\n", *ptr); // 访问空指针
    return 0;
}

运行时输出(可能因系统而异,通常会导致程序崩溃):

Segmentation fault (core dumped)

修正后的代码

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int*)malloc(sizeof(int));
    if(ptr == NULL) {
        perror("Memory allocation failed");
        return 1;
    }

    *ptr = 100;
    printf("Value: %d\n", *ptr);

    free(ptr); // 释放内存
    ptr = NULL; // 防止悬挂指针

    return 0;
}

运行时输出

Value: 100
12.2.3 数组越界

定义:数组越界是指程序尝试访问数组中不存在的元素,可能导致未定义行为、数据损坏或程序崩溃。

示例

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    printf("Sixth element: %d\n", arr[5]); // 数组越界
    return 0;
}

运行时输出(可能因系统而异,通常会输出随机值或导致程序崩溃):

Sixth element: 32767

修正后的代码

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};

    for(int i = 0; i < 5; i++) {
        printf("Element %d: %d\n", i, arr[i]);
    }

    return 0;
}

运行时输出

Element 0: 1
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
12.2.4 内存访问违规

定义:内存访问违规是指程序尝试访问未经授权的内存区域,通常会导致程序崩溃。

示例

#include <stdio.h>

int main() {
    int arr[3] = {10, 20, 30};
    int *ptr = arr + 5; // 指向数组之外的内存
    printf("Value: %d\n", *ptr); // 未定义行为
    return 0;
}

运行时输出(可能因系统而异,通常会导致程序崩溃):

Segmentation fault (core dumped)

修正后的代码

#include <stdio.h>

int main() {
    int arr[3] = {10, 20, 30};
    int *ptr = arr;

    for(int i = 0; i < 3; i++) {
        printf("Value: %d\n", *(ptr + i));
    }

    return 0;
}

运行时输出

Value: 10
Value: 20
Value: 30
12.2.5 逻辑错误

定义:逻辑错误是指程序的逻辑不符合预期,导致程序行为与设计意图不符。编译器和运行时不会检测到逻辑错误,因此需要通过测试和调试发现。

示例

#include <stdio.h>

int main() {
    int a = 5;
    int b = 10;
    int c = a * b; // 预期应该是a + b
    printf("Result: %d\n", c); // 输出: 50
    return 0;
}

预期输出

Result: 15

实际输出

Result: 50

修正后的代码

#include <stdio.h>

int main() {
    int a = 5;
    int b = 10;
    int c = a + b; // 修正为加法
    printf("Result: %d\n", c); // 输出: 15
    return 0;
}

运行时输出

Result: 15
12.3 调试技巧

调试是识别和修复程序错误的重要过程。掌握有效的调试技巧,可以显著提高开发效率和代码质量。本节将介绍两种常用的调试方法:使用printf调试和使用gdb调试工具。

12.3.1 使用printf调试

概述printf调试是一种简单且直接的方法,通过在代码中插入printf语句,输出变量的值和程序的执行流程,以定位错误。

优点

  • 简单易用,不需要额外工具。
  • 适用于快速定位简单错误。

缺点

  • 代码中插入大量printf语句可能影响代码可读性。
  • 对于复杂的错误或多线程程序,printf调试效果有限。

示例:调试数组越界问题。

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int sum = 0;

    for(int i = 0; i <= 5; i++) { // 错误:i <= 5 应为 i < 5
        sum += arr[i];
        printf("i = %d, arr[%d] = %d, sum = %d\n", i, i, arr[i], sum); // 插入printf语句
    }

    printf("Total Sum: %d\n", sum);
    return 0;
}

运行时输出(可能因系统而异,通常会导致程序崩溃):

i = 0, arr[0] = 1, sum = 1
i = 1, arr[1] = 2, sum = 3
i = 2, arr[2] = 3, sum = 6
i = 3, arr[3] = 4, sum = 10
i = 4, arr[4] = 5, sum = 15
i = 5, arr[5] = 32767, sum = 32782
Segmentation fault (core dumped)

分析

  • 通过printf语句,可以看到当i = 5时,程序尝试访问arr[5],这是数组的越界访问。

修正后的代码

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int sum = 0;

    for(int i = 0; i < 5; i++) { // 修正循环条件
        sum += arr[i];
        printf("i = %d, arr[%d] = %d, sum = %d\n", i, i, arr[i], sum);
    }

    printf("Total Sum: %d\n", sum);
    return 0;
}

运行时输出

i = 0, arr[0] = 1, sum = 1
i = 1, arr[1] = 2, sum = 3
i = 2, arr[2] = 3, sum = 6
i = 3, arr[3] = 4, sum = 10
i = 4, arr[4] = 5, sum = 15
Total Sum: 15

详细解释

  • 通过观察printf输出,可以发现循环条件错误,导致数组越界访问。
  • 修正循环条件为i < 5,避免访问不存在的数组元素。
12.3.2 使用gdb调试工具

概述gdb(GNU Debugger)是一个功能强大的调试工具,允许程序员逐步执行程序、设置断点、检查变量值等,以深入分析程序的行为和错误。

优点

  • 强大的功能,适用于复杂的调试需求。
  • 能够动态查看和修改程序状态。
  • 支持多种调试操作,如断点设置、单步执行、变量监视等。

缺点

  • 学习曲线较陡峭,需要一定的学习和实践。
  • 需要在编译时添加调试信息(使用-g选项)。

安装

  • 在大多数 Linux 发行版中,可以通过包管理器安装

    gdb
    

    sudo apt-get install gdb
    

示例:使用gdb调试数组越界问题。

源代码(存在数组越界错误):

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int sum = 0;

    for(int i = 0; i <= 5; i++) { // 错误:i <= 5 应为 i < 5
        sum += arr[i];
    }

    printf("Total Sum: %d\n", sum);
    return 0;
}

编译代码

gcc -g -o array_error array_error.c

调试步骤

  1. 启动gdb

    gdb ./array_error
    
  2. 设置断点

    (gdb) break main
    Breakpoint 1 at 0x40054a: file array_error.c, line 5.
    
  3. 运行程序

    (gdb) run
    Starting program: /home/user/array_error
    
    Breakpoint 1, main () at array_error.c:5
    5       int sum = 0;
    
  4. 逐步执行

    (gdb) next
    6       for(int i = 0; i <= 5; i++) { // 错误的循环条件
    
  5. 进入循环

    (gdb) next
    7           sum += arr[i];
    
  6. 查看变量值

    (gdb) print i
    $1 = 0
    (gdb) print arr[i]
    $2 = 1
    (gdb) print sum
    $3 = 1
    
  7. 继续执行

    (gdb) continue
    Continuing.
    
  8. 程序崩溃

    Program received signal SIGSEGV, Segmentation fault.
    7           sum += arr[i];
    
  9. 查看调用栈

    (gdb) backtrace
    #0  0x0000000000400555 in main () at array_error.c:7
    
  10. 检查错误位置

    (gdb) list
    2   #include <stdio.h>
    3
    4   int main() {
    5       int arr[5] = {1, 2, 3, 4, 5};
    6       int sum = 0;
    7
    8       for(int i = 0; i <= 5; i++) { // 错误:i <= 5 应为 i < 5
    9           sum += arr[i];
    10      }
    11
    12      printf("Total Sum: %d\n", sum);
    13      return 0;
    14  }
    
  11. 修正代码

    • 修改循环条件为i < 5

修正后的代码

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int sum = 0;

    for(int i = 0; i < 5; i++) { // 修正循环条件
        sum += arr[i];
    }

    printf("Total Sum: %d\n", sum);
    return 0;
}

重新编译并运行

gcc -g -o array_fixed array_fixed.c
./array_fixed

运行时输出

Total Sum: 15

详细解释

  • 通过gdb,可以逐步执行程序,观察变量的变化,最终发现并修正了循环条件错误,避免了数组越界访问。

常用gdb命令

命令描述
break <location>在指定位置设置断点。
run启动程序执行。
next执行下一行代码,跳过函数调用。
step执行下一行代码,进入函数调用。
print <variable>打印变量的当前值。
continue继续程序执行,直到下一个断点。
backtrace显示调用栈信息。
list显示当前代码周围的代码。
quit退出gdb
12.4 内存泄漏检测工具

定义:内存泄漏是指程序在动态分配内存后未能释放,导致内存无法被重新利用,最终可能耗尽系统内存。内存泄漏会降低程序性能,甚至导致系统崩溃。

常见原因

  • 忘记调用free释放动态分配的内存。
  • 指针被覆盖或丢失,无法释放内存。
  • 多次释放同一内存块。

检测工具

  • Valgrind:一个强大的内存调试工具,能够检测内存泄漏、未初始化内存使用、越界访问等问题。
  • AddressSanitizer (ASan):GCC 和 Clang 编译器提供的内存错误检测工具。
使用 Valgrind 检测内存泄漏

安装

  • 在大多数 Linux 发行版中,可以通过包管理器安装 Valgrind:

    sudo apt-get install valgrind
    

示例:检测内存泄漏。

源代码(存在内存泄漏):

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int*)malloc(10 * sizeof(int));
    if(ptr == NULL) {
        perror("Memory allocation failed");
        return 1;
    }

    // 初始化数组
    for(int i = 0; i < 10; i++) {
        ptr[i] = i * 2;
    }

    // 忘记释放内存
    // free(ptr);

    return 0;
}

编译代码

gcc -g -o memory_leak memory_leak.c

运行 Valgrind

valgrind --leak-check=full ./memory_leak

Valgrind 输出

==12345== Memcheck, a memory error detector
==12345== Command: ./memory_leak
==12345==
==12345==
==12345== HEAP SUMMARY:
==12345==     in use at exit: 40 bytes in 1 blocks
==12345==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==12345==
==12345== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==12345==    at 0x4C2FB55: malloc (vg_replace_malloc.c:309)
==12345==    by 0x400526: main (memory_leak.c:6)
==12345==
==12345== LEAK SUMMARY:
==12345==    definitely lost: 40 bytes in 1 blocks
==12345==    indirectly lost: 0 bytes in 0 blocks
==12345==      possibly lost: 0 bytes in 0 blocks
==12345==    still reachable: 0 bytes in 0 blocks
==12345==         suppressed: 0 bytes in 0 blocks
==12345==
==12345== For counts of detected and suppressed errors, rerun with: -v
==12345== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

详细解释

  • HEAP SUMMARY:显示程序在退出时仍然占用的堆内存。
  • LEAK SUMMARY:详细列出内存泄漏情况。
  • definitely lost:确定内存泄漏,内存被分配但未释放,且无法再访问。

修正后的代码

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int*)malloc(10 * sizeof(int));
    if(ptr == NULL) {
        perror("Memory allocation failed");
        return 1;
    }

    // 初始化数组
    for(int i = 0; i < 10; i++) {
        ptr[i] = i * 2;
    }

    // 正确释放内存
    free(ptr);

    return 0;
}

重新运行 Valgrind

valgrind --leak-check=full ./memory_leak

Valgrind 输出

==12346== Memcheck, a memory error detector
==12346== Command: ./memory_leak
==12346==
==12346==
==12346== HEAP SUMMARY:
==12346==     in use at exit: 0 bytes in 0 blocks
==12346==   total heap usage: 1 allocs, 1 frees, 40 bytes allocated
==12346==
==12346== All heap blocks were freed -- no leaks are possible
==12346==
==12346== For counts of detected and suppressed errors, rerun with: -v
==12346== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

详细解释

  • All heap blocks were freed:表示所有分配的内存都已正确释放,无内存泄漏。
使用 AddressSanitizer (ASan)检测内存错误

概述:AddressSanitizer 是 GCC 和 Clang 编译器提供的内存错误检测工具,能够检测内存泄漏、缓冲区溢出、使用后释放等问题。

使用方法

  1. 编译代码时启用 ASan

    gcc -g -fsanitize=address -o asan_example asan_example.c
    
  2. 运行程序

    ./asan_example
    

示例:检测内存越界错误。

源代码(存在数组越界错误):

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    arr[5] = 10; // 数组越界
    printf("Value: %d\n", arr[5]);
    return 0;
}

编译代码

gcc -g -fsanitize=address -o asan_example asan_example.c

运行 ASan 检测

./asan_example

ASan 输出

=================================================================
==12347==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffeefbff4a4 at pc 0x0000004006f6 bp 0x7ffeefbff470 sp 0x7ffeefbff468
WRITE of size 4 at 0x7ffeefbff4a4 thread T0
    #0 0x4006f5 in main (/path/to/asan_example+0x4006f5)
    #1 0x7fff2042bd96 in start (/usr/lib/system/libdyld.dylib+0x1bd96)

Address 0x7ffeefbff4a4 is located in stack of thread T0 at offset 20 in frame
    #0 0x4006d4 in main (/path/to/asan_example+0x4006d4)

SUMMARY: AddressSanitizer: stack-buffer-overflow (/path/to/asan_example+0x4006f5) in main
Shadow bytes around the buggy address:
  0x1000ffbff450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000ffbff460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000ffbff470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000ffbff480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000ffbff490: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x1000ffbff4a4: 00 00 00 00 00 00[04]fa fa fa fa fa fa fa fa fa
  0x1000ffbff4b4: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000ffbff4c4: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000ffbff4d4: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000ffbff4e4: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x1000ffbff4f4: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

详细解释

  • stack-buffer-overflow:表示栈上的缓冲区溢出,即数组越界访问。
  • WRITE of size 4:尝试写入 4 字节的数据。
  • Location:指出错误发生的位置和相关调用栈信息。

修正后的代码

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    // 修正数组索引,避免越界
    arr[4] = 10; // 最后一个有效索引是4
    printf("Value: %d\n", arr[4]);
    return 0;
}

重新编译并运行

gcc -g -fsanitize=address -o asan_example asan_example.c
./asan_example

运行时输出

Value: 10

详细解释

  • 通过修改数组索引,避免了越界访问,ASan 不会报告任何错误。
12.4 内存泄漏检测工具

内存泄漏是指程序在动态分配内存后未能释放,导致内存无法被重新利用。内存泄漏会降低程序性能,甚至导致系统内存耗尽,影响其他程序的运行。使用内存泄漏检测工具可以帮助程序员识别和修复这些问题。

12.4.1 Valgrind

概述:Valgrind 是一个开源的内存调试工具,主要用于检测内存泄漏、未初始化内存使用、缓冲区溢出等问题。它通过动态二进制插桩技术,在程序运行时监控内存使用情况。

安装

  • 在大多数 Linux 发行版中,可以通过包管理器安装 Valgrind:

    sudo apt-get install valgrind
    

使用方法

  1. 编译代码时添加调试信息

    gcc -g -o valgrind_example valgrind_example.c
    
  2. 运行程序并使用 Valgrind 检测

    valgrind --leak-check=full ./valgrind_example
    

示例:检测内存泄漏。

源代码(存在内存泄漏):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char name[50];
    int age;
} Person;

int main() {
    Person *p1 = (Person*)malloc(sizeof(Person));
    if(p1 == NULL) {
        perror("Memory allocation failed");
        return 1;
    }
    strcpy(p1->name, "Alice");
    p1->age = 30;

    // 忘记释放p1
    // free(p1);

    return 0;
}

编译代码

gcc -g -o valgrind_example valgrind_example.c

运行 Valgrind

valgrind --leak-check=full ./valgrind_example

Valgrind 输出

==12348== Memcheck, a memory error detector
==12348== Command: ./valgrind_example
==12348==
==12348==
==12348== HEAP SUMMARY:
==12348==     in use at exit: 56 bytes in 1 blocks
==12348==   total heap usage: 1 allocs, 0 frees, 56 bytes allocated
==12348==
==12348== 56 bytes in 1 blocks are definitely lost in loss record 1 of 1
==12348==    at 0x4C2FB55: malloc (vg_replace_malloc.c:309)
==12348==    by 0x4005A5: main (valgrind_example.c:9)
==12348==
==12348== LEAK SUMMARY:
==12348==    definitely lost: 56 bytes in 1 blocks
==12348==    indirectly lost: 0 bytes in 0 blocks
==12348==      possibly lost: 0 bytes in 0 blocks
==12348==    still reachable: 0 bytes in 0 blocks
==12348==         suppressed: 0 bytes in 0 blocks
==12348==
==12348== For counts of detected and suppressed errors, rerun with: -v
==12348== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

详细解释

  • HEAP SUMMARY:显示在程序退出时仍然占用的堆内存。
  • LEAK SUMMARY:详细列出内存泄漏情况。
  • definitely lost:确定的内存泄漏,内存被分配但未释放,且无法再访问。

修正后的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char name[50];
    int age;
} Person;

int main() {
    Person *p1 = (Person*)malloc(sizeof(Person));
    if(p1 == NULL) {
        perror("Memory allocation failed");
        return 1;
    }
    strcpy(p1->name, "Alice");
    p1->age = 30;

    // 正确释放内存
    free(p1);

    return 0;
}

重新运行 Valgrind

valgrind --leak-check=full ./valgrind_example

Valgrind 输出

==12349== Memcheck, a memory error detector
==12349== Command: ./valgrind_example
==12349==
==12349==
==12349== HEAP SUMMARY:
==12349==     in use at exit: 0 bytes in 0 blocks
==12349==   total heap usage: 1 allocs, 1 frees, 56 bytes allocated
==12349==
==12349== All heap blocks were freed -- no leaks are possible
==12349==
==12349== For counts of detected and suppressed errors, rerun with: -v
==12349== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

详细解释

  • All heap blocks were freed:表示所有分配的内存都已正确释放,无内存泄漏。

更多 Valgrind 选项

  • --leak-check=full:进行详细的内存泄漏检查。
  • --show-leak-kinds=all:显示所有类型的内存泄漏。
  • --track-origins=yes:跟踪未初始化内存的来源。
12.5 综合示例:调试与内存泄漏检测

示例:一个包含多个错误的程序,使用printfgdb调试,并使用 Valgrind 检测内存泄漏。

源代码(存在数组越界和内存泄漏):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char name[50];
    int age;
} Person;

int main() {
    Person *p = (Person*)malloc(sizeof(Person));
    if(p == NULL) {
        perror("Memory allocation failed");
        return 1;
    }

    strcpy(p->name, "Bob");
    p->age = 25;

    printf("Name: %s, Age: %d\n", p->name, p->age);

    // 数组越界错误
    int arr[3] = {1, 2, 3};
    for(int i = 0; i <= 3; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    // 忘记释放内存
    // free(p);

    return 0;
}

调试步骤

  1. 使用printf定位错误
    • 在循环中添加printf语句,观察索引i的变化。
    • 发现当i = 3时,尝试访问arr[3],导致数组越界。
  2. 使用gdb调试
    • 设置断点在循环开始处。
    • 逐步执行,观察i的值和arr[i]的访问情况。
    • 确认i <= 3应修正为i < 3
  3. 使用 Valgrind 检测内存泄漏
    • 运行 Valgrind,发现未释放的内存块。

修正后的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char name[50];
    int age;
} Person;

int main() {
    Person *p = (Person*)malloc(sizeof(Person));
    if(p == NULL) {
        perror("Memory allocation failed");
        return 1;
    }

    strcpy(p->name, "Bob");
    p->age = 25;

    printf("Name: %s, Age: %d\n", p->name, p->age);

    // 修正数组越界错误
    int arr[3] = {1, 2, 3};
    for(int i = 0; i < 3; i++) { // 修改条件为i < 3
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    // 正确释放内存
    free(p);

    return 0;
}

Valgrind 重新检测

valgrind --leak-check=full ./valgrind_example

Valgrind 输出

==12350== Memcheck, a memory error detector
==12350== Command: ./valgrind_example
==12350==
Name: Bob, Age: 25
arr[0] = 1
arr[1] = 2
arr[2] = 3
==12350==
==12350== HEAP SUMMARY:
==12350==     in use at exit: 0 bytes in 0 blocks
==12350==   total heap usage: 1 allocs, 1 frees, 56 bytes allocated
==12350==
==12350== All heap blocks were freed -- no leaks are possible
==12350==
==12350== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

详细解释

  • 通过printfgdb,成功定位并修正了数组越界错误。
  • 通过 Valgrind,确认所有动态分配的内存已被正确释放,无内存泄漏。
总结

在 C 语言开发过程中,理解和识别常见的编译错误与运行时错误,以及掌握有效的调试技巧和内存泄漏检测工具,是编写高质量、可靠程序的关键。本章详细介绍了以下内容:

  • 常见编译错误
    • 语法错误、类型错误、未定义的变量或函数、括号不匹配等。
    • 通过代码示例和编译器输出,展示如何识别和修复这些错误。
  • 运行时错误
    • 除以零错误、空指针引用、数组越界、内存访问违规、逻辑错误等。
    • 通过代码示例和运行时输出,展示如何识别和修复这些错误。
  • 调试技巧
    • 使用printf调试:简单直接,适用于快速定位错误。
    • 使用gdb调试工具:功能强大,适用于复杂的调试需求。
  • 内存泄漏检测工具
    • Valgrind:检测内存泄漏、未初始化内存使用、缓冲区溢出等。
    • AddressSanitizer (ASan):编译器提供的内存错误检测工具,集成方便。