浅谈 my_strcmp() 函数

本文详细介绍了如何使用C语言实现字符串比较函数,通过逐步比较每个字符直至找到不匹配处,最终返回比较结果。
//对每一个字符逐步进行比较
#include<stdio.h>
#include <assert.h>

 int my_strcmp(const char *p,const char *q)
 {
     assert(p!=NULL);//断言,括号内假就退出
	 assert(q!=NULL);
	 int i = 0;
	 for(;q[i]== p[i];i++)
	 {
	     if('\0'== q[i])//其实判断条件为'\0'==p[i]也行
		 {
		     return 0;
		 
		 }
	        if((p[i]-q[i])>0)
		  {
		     
		     return 1;
		  
		  }
	           else
			   {
			   
			   return -1;
			   
			   }
	 
	 
	 }
    
 
 
 
 }
int  main( void )
{
    char string[20]="1";//非数字字符串也行 
    char S_string[20]="123456";
  printf("%d\n", my_strcmp( string,S_string ));
   
    return 0;
}

`_dmalloc_strcmp` 是 **DMALLOC 库**中的一个调试版本的 `strcmp` 函数,用于替代标准 C 库中的 `strcmp`,以支持内存错误检测和调试功能。 --- ### ✅ 什么是 DMALLOC? **DMALLOC**(Debug MALLOC)是一个开源的 C 语言内存调试库,用于检测: - 内存泄漏(memory leaks) - 越界访问(buffer overruns/underruns) - 使用已释放内存 - 不匹配的分配/释放(如 `malloc` / `free` 混用 `new` / `delete`) 它通过替换标准内存函数(如 `malloc`, `free`, `strcpy`, `strcmp` 等)为带日志和检查逻辑的调试版本来实现这些功能。 --- ### 🔍 `_dmalloc_strcmp` 函数详解 #### 原型(类似标准 `strcmp`): ```c int _dmalloc_strcmp(const char *s1, const char *s2); ``` 这个函数的行为与标准 `strcmp` 相同:按字典序比较两个字符串,返回负数、0 或正数。 但它的底层实现会: 1. **记录调用上下文**(文件名、行号等) 2. **检查指针合法性**(是否为空?是否指向无效内存?) 3. 可能 **记录字符串内容或长度** 用于后续分析 4. 在出错时输出诊断信息(如传入了未初始化的指针) --- ### 🛠 示例:如何启用 `_dmalloc_strcmp` 你需要在项目中链接 DMALLOC 库,并定义宏来重定向 `strcmp` 到 `_dmalloc_strcmp`。 #### 步骤 1:包含 dmalloc.h 并初始化 ```c #include <stdio.h> #include "dmalloc.h" // 包含 dmalloc 头文件 int main() { // 初始化 dmalloc(必须放在最开始) dmalloc_debug(0x4f7dff); // 启用常见调试选项 char *a = "hello"; char *b = "world"; int result = strcmp(a, b); // 实际调用的是 _dmalloc_strcmp printf("Result: %d\n", result); return 0; } ``` > 注意:当你包含 `"dmalloc.h"` 时,它通常会用宏将 `strcmp` 替换为 `_dmalloc_strcmp`,所以你写的 `strcmp` 实际上调用了 `_dmalloc_strcmp`。 #### 编译并链接 DMALLOC: ```bash gcc -DDMALLOC -DDMALLOC_DEBUG example.c -ldmalloc ``` 或者静态链接 dmalloc。 --- ### 💡 `_dmalloc_strcmp` 的内部行为(简化模拟) 以下是对其可能实现方式的一个**概念性模拟**(非真实源码,仅为说明原理): ```c #include <stdio.h> #include <stdarg.h> // 模拟日志函数 void dmalloc_log(const char *file, int line, const char *func, const char *fmt, ...) { va_list args; fprintf(stderr, "[%s:%d] %s: ", file, line, func); va_start(args, fmt); vfprintf(stderr, fmt, args); va_end(args); fprintf(stderr, "\n"); } // 调试版 strcmp(概念实现) int _dmalloc_strcmp(const char *file, int line, const char *s1, const char *s2) { // 可选:检查指针是否有效(在真实 dmalloc 中会更复杂) if (s1 == NULL || s2 == NULL) { dmalloc_log(file, line, "_dmalloc_strcmp", "NULL pointer passed: s1=%p, s2=%p", s1, s2); // 仍然处理 NULL 避免崩溃,但记录警告 if (s1 == NULL && s2 == NULL) return 0; return (s1 == NULL) ? -1 : 1; } // 执行正常的 strcmp 逻辑 const unsigned char *p1 = (const unsigned char *)s1; const unsigned char *p2 = (const unsigned char *)s2; while (*p1 && (*p1 == *p2)) { p1++; p2++; } int diff = *p1 - *p2; // 可选:记录比较操作(用于调试跟踪) // dmalloc_log(file, line, "_dmalloc_strcmp", "compared '%s' vs '%s', result=%d", s1, s2, diff); return diff; } ``` 然后通过宏替换让所有 `strcmp` 调用变成带文件/行号的 `_dmalloc_strcmp`: ```c #define strcmp(s1, s2) _dmalloc_strcmp(__FILE__, __LINE__, (s1), (s2)) ``` 这就是 dmalloc 实现“无侵入式”监控的方式。 --- ### ⚠️ 注意事项 - `_dmalloc_strcmp` 不是标准函数,只存在于使用 DMALLOC 的构建环境中。 - 它会影响性能(每次调用都有额外开销),仅应在调试阶段使用。 - 发布版本应关闭 DMALLOC 宏定义,恢复使用原生 `strcmp`。 --- ### 如何查看真实 DMALLOC 源码? 你可以从官方仓库查看 `_dmalloc_strcmp` 的真实实现: 🔗 [https://github.com/westes/dmalloc](https://github.com/westes/dmalloc) 搜索关键词:`_dmalloc_strcmp` 或 `FUNCTION_STRCMP` --- ### 总结 | 特性 | 说明 | |------|------| | `_dmalloc_strcmp` 是什么? | DMALLOC 提供的调试版 `strcmp` | | 是否替代标准 `strcmp`? | 是,通过宏定义自动替换 | | 主要用途 | 检测传入字符串指针的合法性,记录调用栈 | | 是否影响性能? | 是,仅用于调试环境 | | 是否调用 `strcoll`? | 否,它仍然是基于字节的比较,不会涉及 locale | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值