_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、コピーstrSourceの記憶域スペースを割り当て、割り当てられた領域にコピーstrSourceするために呼び出mallocします。

_wcsdup 関数と _mbsdup 関数は、_strdup 関数のワイド文字バージョンとマルチバイト文字バージョンです。 引数と戻り値 _wcsdup はワイド文字列です。 引数と戻り値 _mbsdup はマルチバイト文字列です。 それ以外では、これらの関数の動作は同じです。

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

汎用テキスト ルーチンのマップ

TCHAR.H ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_tcsdup _strdup _mbsdup _wcsdup

コピー用の記憶域スペースを割り当てる呼び出しは_strdup、常に呼び出mallocしによって返されるポインターでルーチンをfree呼び出すことによって、このメモリを_strdup解放することをお勧めします。strSource

_CRTDBG_MAP_ALLOC定義されている場合_DEBUG_strdupおよび_wcsdupメモリ割り当てのデバッグを可能にする呼び出_strdup_dbgしに_wcsdup_dbg置き換えられます。 詳細については、「 _strdup_dbg」と「 _wcsdup_dbgの両方を管理できます。

必要条件

ルーチンによって返される値 必須ヘッダー
_strdup <string.h>
_wcsdup <string.h> または <wchar.h>
_mbsdup <mbstring.h>

互換性の詳細については、「 Compatibility」を参照してください。

// 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

関連項目

文字列操作
memset, wmemset
strcat, wcscat, _mbscat
strcmp, wcscmp, _mbscmp
strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strspn, wcsspn, _mbsspn, _mbsspn_l