Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is wmemcpy_s in C?

Abdul Monum

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

wmemcpy_s in C copies wide characters from a source buffer to a destination buffer. wmemcpy_s performs the same operation as wmemcpy, but in addition, wmemcpy_s validates its parameters and detects errors at runtime. wmemcpy_s is defined in the wchar.h header and its function declaration in the standard C library is as follows:

errno_t wmemcpy_s(wchar_t *restrict dest, rsize_t destsz, const wchar_t *restrict src, rsize_t count);

NOTE: A wide string uses characters from the Unicode character set, where typically each character is of 2 bytes.

Parameters

dest: Destination buffer

destsz: Maximum size of the destination buffer in bytes

src: Source buffer consisting of wide characters

count: Number of wide characters to copy

Return value

  • Upon success: Zero

  • Upon failure: Error code

Error handling

  • If non-zero count and src or dest buffer is NULL, then EINVAL error code is returned.
  • If count is greater than destsz, then ERANGE error code is returned.
  • If count or destsz is greater than RSIZE_MAX/sizeof(wchar_t), then ERANGE error code is returned.
  • If src and dest overlap, then behavior of wmemcpy_s is undefined

Example

#include <stdio.h>
#include <wchar.h>
int main()
{
wchar_t src[40] = L"How to use wmemcpy_s in C";
wchar_t dest[40];
if (!wmemcpy_s(dest, sizeof(dest), src, 18))
{
wprintf(L"%ls\n", dest);
}
else
{
printf("Error occured");
}
return 0;
}

Output

How to use wmemcpy

NOTE: wmemcpy_s is not supported by the GCC compiler, so you will get an implicit declaration of function… error. Use the following variant to get the job done: wmemcpy.

Explanation

First, we import the wchar.h header file. In lines 6-7, we initialize the destination and source buffer, where the L identifier before the string indicates that the characters to follow are from the Unicode character set. In line 9, we copy 18 wide characters from src to dest using wmemcpy_s, which returns 0 on success, and the wprintf statement in line 10 outputs the contents of dest.

NOTE: If there is a memory overlap region between src and dest, then you should use wmemmove_s instead, as it performs the same operation as wmemcpy_s and handles the overlap correctly.

RELATED TAGS

c

CONTRIBUTOR

Abdul Monum
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring