C++string函数之strcpy_s

strcpy_s和strcpy()函数的功能几乎是一样的。strcpy函数,就象gets函数一样,它没有方法来保证有效的缓冲区尺寸,所以它只能假定缓冲足够大来容纳要拷贝的字符串。在程序运行时,这将导致不可预料的行为。用strcpy_s就可以避免这些不可预料的行为。

strcpy_s是strcpy的安全版本,它之所以安全,是因为其在拷贝字符串的时候会有越界的检查工作。以下是strcpy_s的实现代码,在tcscpy_s.inl文件可以找到:

/***
*tcscpy_s.inl - general implementation of _tcscpy_s
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       This file contains the general algorithm for strcpy_s and its variants.
*
****/

_FUNC_PROLOGUE
errno_t __cdecl _FUNC_NAME(_CHAR *_DEST, size_t _SIZE, const _CHAR *_SRC)
{
    _CHAR *p;
    size_t available;

    /* validation section */
    _VALIDATE_STRING(_DEST, _SIZE);
    _VALIDATE_POINTER_RESET_STRING(_SRC, _DEST, _SIZE);

    p = _DEST;
    available = _SIZE;
    while ((*p++ = *_SRC++) != 0 && --available > 0)
    {
    }

    if (available == 0)
    {
        _RESET_STRING(_DEST, _SIZE);
        _RETURN_BUFFER_TOO_SMALL(_DEST, _SIZE);
    }
    _FILL_STRING(_DEST, _SIZE, _SIZE - available + 1);
    _RETURN_NO_ERROR;
}

_VALIDATE_STRING应该是验证字符串的合法性,是否以null结尾。

_VALIDATE_POINTER_RESET_STRING应该是记录字符串的原始信息,以便拷贝失败以后恢复。

当目的地空间不够时,会根据_VALIDATE_POINTER_RESET_STRING记录的信息恢复字符串,并且(在Debug模式下)以弹出对话框的形式报告错误。

_FILL_STRING完成在字符串最后加上null结束符的工作。

正确用法:

int n = 6;
  char **argsmy = new char *[n];
  int maxlen = 600;
  for(int i = 0; i < n; i ++)
  {
   argsmy[i] = new char [maxlen];// args[i];
  }
   strcpy_s(argsmy[1],maxlen,"e");
  strcpy_s(argsmy[2],maxlen,"Lzma_");
  strcat_s(argsmy[2], 600, cTAppEncTop.getBitstreamFile());
  strcpy_s(argsmy[3],maxlen,"-BS12");
  strcpy_s(argsmy[4],maxlen,"-CN0");
  strcpy_s(argsmy[5],maxlen,"-d15");

错误用法:

argsmy[2] = "Lzma_"; strcpy_s(argsmy[2],maxlen,"see");

原因:

argsmy[2] = "Lzma_"; //因为 argsmy[2] 是个指针。他指向一块分配的空间 ,长度 maxlen。

而这样赋值后,指针指向位置变了。而再strcpy_s(argsmy[2],maxlen,"see"); 实际上是将常数变量空间强制赋值。因此出问题。

strcpy_s 用法:

errno_t strcpy_s(
  char *strDestination,
  size_t numberOfElements,
  const char *strSource
); 

template <size_t size>
errno_t strcpy_s(
  char (&strDestination)[size],
  const char *strSource
); // C++ only

例子:

// crt_strcpy_s.cpp
// This program uses strcpy_s and strcat_s
// to build a phrase.
//

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

int main( void )
{
   char string[80];
   // using template versions of strcpy_s and strcat_s:
   strcpy_s( string, "Hello world from " );
   strcat_s( string, "strcpy_s " );
   strcat_s( string, "and " );
   // of course we can supply the size explicitly if we want to:
   strcat_s( string, _countof(string), "strcat_s!" );

   printf( "String = %s/n", string );
}
时间: 2024-08-06 05:41:19

C++string函数之strcpy_s的相关文章

PHP String 函数

PHP String 简介 String 字符串函数允许您对字符串进行操作. 安装 String 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP String 函数 PHP:指示支持该函数的最早的 PHP 版本. 函数 描述 PHP addcslashes() 在指定的字符前添加反斜杠. 4 addslashes() 在指定的预定义字符前添加反斜杠. 3 bin2hex() 把 ASCII 字符的字符串转换为十六进制值. 3 chop() rtrim() 的别名. 3 chr

string函数分析

string函数分析string函数包含在string.c文件中,经常被C文件使用.1. strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把str2指向的字符串拷贝到str1中去函数返回: 返回str1,即指向str1的指针 /** * strcpy - Copy a %NUL terminated string * @dest: Where to copy the string to * @src: Where to copy the

c plus plus的string函数实现(希望高手路过指点一二)

本来可以轻松搞定的,可惜遇到一个暂时解决不了的问题,没有任何提示的崩; #ifndef _MYSTING_h_ #define _MYSTING_h_ /* String类; */ using namespace std ; /************************************************************************/ /*访函数,好处:相当于闭包,使得函数能够使用绑定的局部变量(成员变量) 用于函数重载时比较清晰 其它(待补充)*/ /**

PHP 5 String 函数

PHP 5 String 函数 PHP String 函数是 PHP 核心的组成部分.无需安装即可使用这些函数.http://www.jinnan411.top/ 函数 描述 addcslashes() 返回在指定的字符前添加反斜杠的字符串. addslashes() 返回在预定义的字符前添加反斜杠的字符串. bin2hex() 把 ASCII 字符的字符串转换为十六进制值. chop() 移除字符串右侧的空白字符或其他字符. chr() 从指定 ASCII 值返回字符. chunk_split

c++:string函数

string类的构造函数:string(const char *s);    //用c字符串s初始化string(int n,char c);     //用n个字符c初始化此外,string类还支持默认构造函数和复制构造函数,如string s1:string s2="hello":都是正确的写法.当构造的string太长而无法表达时会抛出length_error异常 string类的字符操作:const char &operator[](int n)const;const

string函数在字符串和数值之间转换的应用

#include<iostream> #include<string> #include<sstream> using namespace std ; int main() { string line ; while(getline(cin,line)) { int sum=0 , x ; stringstream ss(line) ; while(ss>>x) sum+=x ; cout<<sum<<endl ; } return

redis 字符串(string)函数

字符串(string)函数 get 命令/方法/函数 Description Get the value related to the specified key 取得与指定的键值相关联的值 Parameters key Return Value String or Bool: If key didn't exist, FALSE is returned. Otherwise, the value related to this key is returned. 返回相关值或者BOOL值,如果K

C# String函数

public static bool IsNullOrEmpty(string value) 如果 true 参数为 value 或空字符串 (""),则为 null:否则为 false. 返回组合后的字符串 decimal temp = 20.4m; string s = String.Format("The temperature is {0}°C.", temp);Console.WriteLine(s);// Displays 'The temperatur

PHP String函数分类

1.查找字符位置函数: strpos  ($str,search,[int]):    查找search在$str中的第一次位置从int开始: stripos ($str,search,[int]):    函数返回字符串在另一个字符串中第一次出现的位置.该函数对大小写不敏感 strrpos ($str,search,[int]):    查找search在$str中的最后一次出现的位置从int 2.提取子字符函数(双字节) submit ($str,int start[,int length]