当前位置: 首页 > news >正文

网站推荐软件怎么提高百度搜索排名

网站推荐软件,怎么提高百度搜索排名,建设个人网站第一步这么做,做网站 找风投C 语言中常用的调试技巧和 demo C语言中常用的调试方法 打印调试信息 GDB 调试器 编写单元测试 段错误产生原因 初学时两种常用的段错误调试方法 C 语言中常用的调试技巧和 demo 当程序员进行调试时,他们通常会使用一些调试语句或技巧来帮助他们理解代码的执行过程…

C 语言中常用的调试技巧和 demo
C语言中常用的调试方法
打印调试信息
GDB 调试器
编写单元测试
段错误产生原因
初学时两种常用的段错误调试方法

C 语言中常用的调试技巧和 demo

当程序员进行调试时,他们通常会使用一些调试语句或技巧来帮助他们理解代码的执行过程以及识别问题。以下是一些在 C 语言中常用的调试技巧和 demo:

1. 使用 printf 进行调试

#include <stdio.h>int main() {int a = 5;printf("Value of a: %d\n", a);// ...return 0;
}

2. 使用 assert 进行断言

#include <assert.h>int divide(int a, int b) {assert(b != 0);return a / b;
}

3. 使用 gdb 进行命令行调试

gcc -g -o my_program my_program.c
gdb ./my_program

在 GDB 中可以使用诸如 break, run, print, step, backtrace 等命令进行调试。

4. 输出变量地址

#include <stdio.h>int main() {int a = 5;printf("Address of a: %p\n", (void*)&a);// ...return 0;
}

5. 使用 #ifdef 和宏定义进行条件编译

#include <stdio.h>#define DEBUG 1int main() {#ifdef DEBUGprintf("Debugging information\n");#endif// ...return 0;
}

6. 手动触发程序崩溃

#include <stdio.h>
#include <stdlib.h>int main() {int* ptr = NULL;*ptr = 5;  // This will cause a segmentation fault// ...return 0;
}

7. 使用 errno 打印错误信息

#include <stdio.h>
#include <errno.h>int main() {FILE *file = fopen("nonexistent_file.txt", "r");if (file == NULL) {perror("Error opening file");}// ...return 0;
}

8. 使用 valgrind 进行内存检查

valgrind ./my_program

Valgrind 可以检测内存泄漏和其他内存错误。

这些都是简单而有效的调试技巧,可以帮助你更好地理解代码并找到潜在问题。

C语言中常用的调试方法

在C语言中,常用的调试方法包括打印调试信息、使用调试器和编写单元测试等。下面是一些用于调试的示例代码:

  1. 打印调试信息
#include <stdio.h>// 在代码中插入打印语句
void example_function() {printf("Debug: This is a debug message.\n");// ... 其他代码 ...
}
  1. 条件打印
#include <stdio.h>// 使用条件打印语句
#ifdef DEBUG
#define DEBUG_PRINT(fmt, args...) printf(fmt, ##args)
#else
#define DEBUG_PRINT(fmt, args...) // 空定义,即不执行任何操作
#endifvoid example_function() {DEBUG_PRINT("Debug: This is a debug message.\n");// ... 其他代码 ...
}

在编译时,可以通过定义或未定义 DEBUG 宏来控制是否打印调试信息。

  1. 使用 assert 断言
#include <assert.h>void example_function(int x) {assert(x > 0 && "x must be greater than 0");// ... 其他代码 ...
}

assert 宏用于检查一个表达式是否为真,如果为假,则触发一个断言失败。

  1. 使用调试器(GDB)
#include <stdio.h>int main() {int x = 10;int y = 0;// 计算除法,可能导致除零错误int result = x / y;printf("Result: %d\n", result);return 0;
}

在终端中使用 GDB 调试程序:

gcc -g -o my_program my_program.c
gdb my_program

在 GDB 中运行程序,当程序崩溃时,可以使用 backtrace 和其他命令来查看调用栈和变量值。

  1. 编写单元测试

使用测试框架进行单元测试,例如 C 中的 Unity 框架。编写测试用例来验证函数的预期行为。

#include "unity.h"void test_addition() {TEST_ASSERT_EQUAL(5, add(2, 3));
}int main() {UNITY_BEGIN();RUN_TEST(test_addition);return UNITY_END();
}

以上示例代码提供了一些常见的调试方法。选择合适的方法取决于问题的性质和复杂性。

打印调试信息

在C语言中,开发者经常使用printf语句进行调试。下面是一些在不同情境下用于调试的printf语句的示例:

  1. 打印变量值

    int x = 42;
    printf("The value of x is: %d\n", x);
    
  2. 调试文件、函数、行号

    printf("%s|%s|%d\n", __FILE__, __func__, __LINE__);
    
  3. 打印字符串

    char message[] = "Hello, World!";
    printf("Message: %s\n", message);
    
  4. 打印指针地址

    int *ptr = NULL;
    printf("Pointer address: %p\n", (void*)ptr);
    
  5. 条件打印

    #ifdef DEBUGprintf("Debug information\n");
    #endif
    
  6. 格式化输出

    double pi = 3.14159265359;
    printf("Value of pi: %.2f\n", pi);  // 保留两位小数
    
  7. 调试标志

    #define DEBUG 1#if DEBUGprintf("Debugging is enabled\n");
    #endif
    
  8. 打印多个变量

    int a = 10, b = 20;
    printf("Values: a=%d, b=%d\n", a, b);
    
  9. 打印特殊字符

    printf("This is a newline: \n");
    
  10. 查看函数执行流程

    printf("Entering function...\n");
    // Function code
    printf("Leaving function...\n");
    

这些语句可以根据需要添加或组合,根据代码的结构和特定的调试需求选择合适的调试语句。在实际的开发中,有时候也会使用专门的调试工具,比如gdb(GNU Debugger)等。

GDB 调试器

GDB(GNU Debugger)是一款强大的调试器,可以用于调试C、C++等程序。以下是一些基本的 GDB 使用方法:

编译程序以便调试

在编译程序时,需要包含调试信息。使用 -g 选项告诉编译器生成调试信息:

gcc -g -o my_program my_program.c

启动 GDB

gdb ./my_program

基本命令

  1. 运行程序

    run
    

    或者可以带参数:

    run arg1 arg2
    
  2. 设置断点

    在函数或者某一行设置断点:

    break function_name
    break file_name:line_number
    
  3. 执行程序直到断点

    continue
    
  4. 单步执行

    next      # 执行一行代码,不进入函数
    step      # 执行一行代码,进入函数
    
  5. 查看变量值

    print variable_name
    
  6. 查看函数调用栈

    backtrace
    
  7. 跳转到某一行

    jump line_number
    
  8. 终止程序

    kill
    

示例

考虑以下简单的程序:

// my_program.c
#include <stdio.h>int main() {int x = 5;int y = 0;int result = x / y;printf("Result: %d\n", result);return 0;
}

使用 GDB 调试:

  1. 编译程序:

    gcc -g -o my_program my_program.c
    
  2. 启动 GDB:

    gdb ./my_program
    
  3. 在 GDB 中运行程序:

    run
    

    程序将在除以零的地方崩溃。

  4. 查看变量值:

    print x
    print y
    
  5. 查看函数调用栈:

    backtrace
    
  6. 设置断点并运行:

    break main
    run
    

    main 函数的入口设置了断点,程序会在进入 main 函数时停下。

  7. 单步执行:

    step
    

    逐行执行代码,可以看到在除以零的地方停下。

  8. 退出 GDB:

    quit
    

这只是 GDB 的基本用法。对于更复杂的程序,可能需要更多高级的调试技术。 GDB 的命令和功能非常丰富,可以根据需要查阅 GDB 的文档。

编写单元测试

编写单元测试是一种验证代码的方法,以确保每个独立单元的功能都按预期工作。下面是一份简要的指南,帮助你编写 C 语言程序的单元测试。

选择测试框架

在 C 语言中,一些流行的测试框架包括:

  1. Check: Check Testing Framework
  2. Unity: Unity Testing Framework
  3. Google Test: Google Test

安装测试框架

下载并按照测试框架的文档安装框架。通常,这涉及到将测试框架的头文件和库文件添加到你的项目中。

编写测试用例

使用测试框架编写测试用例。测试用例是检查特定函数或模块的代码块,确保其行为符合预期。

示例测试用例(使用 Unity 框架):

#include "unity.h"
#include "my_functions.h"  // 你的代码文件头文件void test_addition() {TEST_ASSERT_EQUAL_INT(5, add(2, 3));
}void test_subtraction() {TEST_ASSERT_EQUAL_INT(2, subtract(5, 3));
}int main() {UNITY_BEGIN();RUN_TEST(test_addition);RUN_TEST(test_subtraction);UNITY_END();return 0;
}

编写被测试的代码

在编写测试用例之前,首先编写要测试的代码。例如,你可能有一个包含两个函数的头文件 my_functions.h

// my_functions.h
#ifndef MY_FUNCTIONS_H
#define MY_FUNCTIONS_Hint add(int a, int b);
int subtract(int a, int b);#endif

对应的实现文件 my_functions.c 可能如下所示:

// my_functions.c
#include "my_functions.h"int add(int a, int b) {return a + b;
}int subtract(int a, int b) {return a - b;
}

构建测试

构建测试时,需要链接测试框架和被测试的代码。

gcc -o my_tests my_tests.c my_functions.c -lunity

运行测试

./my_tests

测试框架将执行所有测试用例,并显示每个测试用例的结果。

分析测试结果

测试通过时,你会看到一条消息表明所有测试用例都通过。如果有测试失败,框架通常会提供详细的输出,说明哪个测试用例失败以及失败的原因。

增加测试覆盖率

编写测试用例时,尽量涵盖不同的输入情况,以确保代码的鲁棒性。覆盖率工具(例如 gcov)可以帮助你确定代码中哪些部分被测试覆盖。

持续集成

将单元测试集成到你的持续集成(CI)流程中,以确保每次代码更改都会触发测试。这有助于捕获引入错误的更改,并确保代码库的整体稳定性。

以上是一个简单的单元测试流程,实际情况可能因项目的复杂性而有所不同。

段错误产生原因

1.访问不存在的内存地址

如下面代码,ptr没有申请空间就直接拷贝数据:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>int main(int argc, char *argv[])
{char *ptr = NULL;//This is the wrong implementation:strncpy(ptr, "abc", 3);//ptr没有申请空间就直接使用//The right way://ptr = (char *)malloc(sizeof(char) * 10);//memset(ptr, 0, 10);//strncpy(ptr, "abc", 3);return 0;
}

2.访问只读的内存地址

错误做法:往字符串常量空间拷贝新的数据

#include <stdlib.h>
#include <stdio.h>
#include <string.h>int main(int argc, char *argv[])
{char *ptr = "test";strcpy(ptr, "TEST1");return 0;
}

3.访问系统保护的内存地址

如:

#include <stdio.h>int main(int argc, char *argv[])
{int *ptr = (int *)0;*ptr = 100;return 0;
}

4.栈溢出

如:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>int main(int argc, char *argv[])
{main(argc, argv);
}

初学时两种常用的段错误调试方法

1.使用printf输出调试信息

这个是看似最简单但往往很多情况下是十分有效的调试方式,也许可以说是程序员用的最多的调试方式。简单来说,就是在程序的重要代码附近加上printf输出信息,这样可以跟踪并打印出段错误在代码中最可能出现的位置。

使用案例:
以下面这段错误代码为例:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>int main(int argc, char *argv[])
{char *ptr = NULL;//This is the wrong implementation:strncpy(ptr, "abc", 3);//ptr没有申请空间就直接使用return 0;
}

可以在代码中添加printf调试信息:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>int main(int argc, char *argv[])
{printf("line:%d\n", __LINE__); //单纯打印代码行数char *ptr = NULL;//This is the wrong implementation:printf("line:%d\n", __LINE__); //单纯打印代码行数strncpy(ptr, "abc", 3);//ptr没有申请空间就直接使用printf("line:%d\n", __LINE__); //单纯打印代码行数return 0;
}

编译运行后,会有如下输出:

line:7
line:10
Segmentation fault (core dumped)

通过日志信息,就可以看到strncpy(ptr, “abc”, 3);语句没有被执行,我们就可以根据定位到的位置来排除ptr是否是未分配空间或者分配的空间不足引发的问题

2.使用gcc和gdb

调试步骤:
1、为了能够使用gdb调试程序,在编译阶段加上-g参数,还是以下面这段错误代码为例:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>int main(int argc, char *argv[])
{char *ptr = NULL;//This is the wrong implementation:strncpy(ptr, "abc", 3);//ptr没有申请空间就直接使用return 0;
}

编译代码添加-g选项

gcc -g -o test test.c

编译出test程序,然后用gdb调试:

linux@linux:~/test$ gdb ./test
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...done.
(gdb) r
Starting program: /home/linux/test/test Program received signal SIGSEGV, Segmentation fault.
0x0000555555554611 in main (argc=1, argv=0x7fffffffe528) at test.c:9
9                               strncpy(ptr, "abc", 3);//ptr没有申请空间就直接使用
(gdb) quit
A debugging session is active.Inferior 1 [process 15829] will be killed.Quit anyway? (y or n) y

从输出看出,程序到SIGSEGV信号,触发段错误,并提示地址0x0000555555554611以及代码第9行出错。

当然,还有利用core文件和gdb配置配合调试、使用objdump以及catchsegv命令的其他更为复杂的调试手段。

http://www.dinnco.com/news/70993.html

相关文章:

  • 美国网站建设公司哪家好游戏优化
  • 有口碑的大良网站建设北京网站优化推广公司
  • 优速网站建设工作室常用的搜索引擎
  • 深圳关键词seoseo网站优化培训价格
  • 孝义做网站阿里云域名查询和注册
  • google建立网站重庆seo教程博客
  • 货代怎么找客户seo优化及推广如何运营
  • 杨思网站建设公司广州灰色优化网络公司
  • 怎样用百度做网站优化上海站优云网络科技有限公司
  • 京东网站建设的经费预算挖掘爱站网
  • 网上停车场做施工图人员网站网络营销好学吗
  • 网站节日制作手机app免费下载
  • cad做兼职区哪个网站跨境电商平台有哪些
  • 西安市人民政府seo渠道
  • 职友集一家做公司点评的网站企业整站推广
  • 网站开发兼职合同网络推广哪个平台好
  • 上海网站建设价格网络营销软件条件
  • 企业网站建设公司郑州游戏代理怎么做
  • 东莞专业做淘宝网站建设网上写文章用什么软件
  • 深圳知名网站建设供应独立站搭建要多少钱
  • 知名做网站的公司世界足球排名前十名
  • 什么是网站平台开发工具国内可访问的海外网站和应用
  • 东川网站建设盐城seo网站优化软件
  • 怎么做qq钓鱼网站软件推广赚佣金渠道
  • 做街舞网站的素材百度起诉seo公司
  • 服装厂做1688网站效果好不好企业网站推广有哪些
  • 东平做网站百度账户代运营
  • 宁德蕉城住房和城乡建设部网站百度推广开户电话
  • 网站建设约谈表态发言关键词热度查询工具
  • 织梦网站怎么做索引地图网络推广搜索引擎