Exportar (0) Imprimir
Expandir todo
div
eof
Expandir Minimizar

_strdup, _wcsdup, _mbsdup

Duplicate strings.


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

Parameters

strSource

Null-terminated source string.

Each of these functions returns a pointer to the storage location for the copied string or NULL if storage cannot be allocated.

The _strdup function calls malloc to allocate storage space for a copy of strSource and then copies strSource to the allocated space.

_wcsdup and _mbsdup are wide-character and multibyte-character versions of _strdup. The arguments and return value of _wcsdup are wide-character strings; those of _mbsdup are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined

_tcsdup

_strdup

_mbsdup

_wcsdup

Because _strdup calls malloc to allocate storage space for the copy of strSource, it is good practice always to release this memory by calling the free routine on the pointer returned by the call to _strdup.

If _DEBUG and _CRTDBG_MAP_ALLOC are defined, _strdup and _wcsdup are replaced by calls to _strdup_dbg and _wcsdup_dbg to allow for debugging memory allocations. For more information, see _strdup_dbg, _wcsdup_dbg.

Routine Required header Compatibility

_strdup

<string.h>

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

_wcsdup

<string.h> or <wchar.h>

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

_mbsdup

<mbstring.h>

Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

// 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 );
}

Output

Original: This is the buffer text
Copy:     This is the buffer text

Adiciones de comunidad

AGREGAR
Mostrar:
© 2014 Microsoft