使用strcat_s的注意事项
来源:互联网 发布:2017中国信贷规模数据 编辑:程序博客网 时间:2024/04/29 06:19
我们要合并字符串的话,使用c语言编写的时候需要注意几点事项。
strcat_s函数声明:
errno_t strcat_s( char *strDestination, size_t numberOfElements, const char *strSource );
出现歧义的大部分为第2个参数。
1. L"Buffer is too small" && 0
当此参数被赋值为下面几种情况下,会发生。
(1)numberOfElements=sizeof(dst)
strcat_s(ret, sizeof(ret), str1);(2)numberOfElements=strlen(src)
strcat_s(ret, strlen(str1), str1);
此错误提示我们目标(Buffer)过小。实际上第二个参数是合并字符串后的字符数量。
即,源串大小 + 目标串大小 + 字符串结束符大小("\0")
第(1)个错误只计算了目标串的大小.
第(2)个错误只计算了源串的大小.
2. L"String is not null terminated" && 0
当我们没有初始化字符串的时候,就会出现。
解决办法:
memset(ret, 0, sizeof(ret));
此演示程序在VS2005调试.
// strcatTest.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdlib.h> // malloc()#include <string.h> // strcat_s() && strlen()int _tmain(int argc, _TCHAR* argv[]){char *ret = (char*)malloc(120);memset(ret, 0, sizeof(ret));char *str1 = "This is a demonstration program, ";char *str2 = "considerations for using strcat_s.";int len1 = strlen(str1) + 1;strcat_s(ret, len1, str1);//strcat_s(ret, sizeof(ret), str1); // Debug Assertion Failed//strcat_s(ret, strlen(str1), str1); // Program: ... // File: .\tcscat_s.inl // Line: 42 // Expression: (L"Buffer is too small" && 0) strcat_s(ret, strlen(str1) + 1, str1); int len2 = strlen(ret) + strlen(str2) + 1; strcat_s(ret, len2, str2); printf("%s", ret); return 0;}
参考文章:
1. strcat_s --- MSDN
3 0
- 使用strcat_s的注意事项
- strcpy_s、sptintf_s与strcat_s的使用
- strcat_s使用问题
- strcpy_s、strcat_s的用法
- strcat_s 和 strcat 的区别
- strcat_s 和 strcat 的区别
- strcpy_s 和 strcat_s的用法
- strcpy_s 和 strcat_s的用法
- strcpy_s 和 strcat_s的用法
- error C3861: “strcat_s”: 即使使用参数相关的查找,也未找到标识符
- sprintf_s和strcat_s函数的感悟
- C++笔记 strcat_s和strcat的区别
- strcat_s详解
- HashMap使用的注意事项
- vss2005使用的注意事项
- vss2005使用的注意事项
- 使用接口的注意事项
- textbox的使用------注意事项
- StatSvn 统计SVN代码信息
- 【暴力贪心】最少拦截系统
- JNDI基本应用----摘自javaweb邮件开发详解
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
- 4
- 使用strcat_s的注意事项
- 如何用js写一个消息提示中心
- input text的电话号码分割js(如:151-9456-7894)
- R language簡介(上)
- 关于架构师的一些文章
- 模式识别(三):线性判别函数
- ns2中的队列,Packet
- nagios 监控Linux 主机
- Destoon二次开发的文档收集