_strdup, _wcsdup, _mbsdup

复制字符串。

重要

_mbsdup 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

char *_strdup(
   const char *strSource
);
wchar_t *_wcsdup(
   const wchar_t *strSource
);
unsigned char *_mbsdup(
   const unsigned char *strSource
);

参数

strSource
null 终止的源字符串。

返回值

如果无法分配存储,则其中每个函数都将返回一个指向复制字符串的存储位置的指针或 NULL

备注

_strdup 函数调用 malloc 来为 strSource 的副本分配存储空间,然后将 strSource 复制到分配的空间。

_wcsdup_mbsdup 分别是 _strdup 的宽字符及多字节字符版本。 _wcsdup 的参数和返回值为宽字符字符串。 _mbsdup 的参数和返回值为多字节字符字符串。 否则这三个函数否则具有相同行为。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcsdup _strdup _mbsdup _wcsdup

因为 _strdup 调用 malloc 来为 strSource 的副本分配存储空间,所以最好始终通过调用由对 _strdup 的调用返回的指针上的 free 例程来释放此内存。

如果定义了 _DEBUG_CRTDBG_MAP_ALLOC 时,则对 _strdup_wcsdup 的调用将替代对 _strdup_dbg_wcsdup_dbg 的调用,以便调试内存分配。 有关详细信息,请参阅 _strdup_dbg_wcsdup_dbg

要求

例程 必需的标头
_strdup <string.h>
_wcsdup <string.h><wchar.h>
_mbsdup <mbstring.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_strdup.c

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

int main( void )
{
   char buffer[] = "This is the buffer text";
   char *newstring;
   printf( "Original: %s\n", buffer );
   newstring = _strdup( buffer );
   printf( "Copy:     %s\n", newstring );
   free( newstring );
}
Original: This is the buffer text
Copy:     This is the buffer text

另请参阅

字符串操作
memsetwmemset
strcatwcscat_mbscat
strcmpwcscmp_mbscmp
strncat_strncat_lwcsncat_wcsncat_l_mbsncat_mbsncat_l
strncmpwcsncmp_mbsncmp_mbsncmp_l
strncpy_strncpy_lwcsncpy_wcsncpy_l_mbsncpy_mbsncpy_l
_strnicmp_wcsnicmp_mbsnicmp_strnicmp_l_wcsnicmp_l_mbsnicmp_l
strrchrwcsrchr_mbsrchr_mbsrchr_l
strspnwcsspn_mbsspn_mbsspn_l