Wmemmove Function In C++ - C++ Programming Tutorial
C++ Course / Functions / Wmemmove Function In C++

Wmemmove Function In C++

BLUF: Mastering Wmemmove Function In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Wmemmove Function In C++

C++ is renowned for its efficiency. Learn how Wmemmove Function In C++ enables low-level control and high-performance computing in the tutorial below.

C++ provides programmers with the capability to create robust applications, establishing itself as one of the most potent and adaptable programming languages available. Within the wide array of functions in C++, wmemmove stands out as a valuable tool for managing the movement of blocks of data within arrays of similar types. This detailed guide delves into the intricacies of remove, exploring its functionality, practical applications, and includes illustrative examples along with the resulting output.

Syntax:

Let's initially break down the structure of wmemmove to enhance our comprehension of its elements:

Example

wchar_t* wmemmove(wchar_t* dest, const wchar_t* src, size_t count);

The destination addresses in memory where the data should be positioned.

Identification of a pointer to a data origin for duplication.

count: The count of characters to be copied.

Wmemmove proves to be particularly valuable in scenarios where there is an overlap between the source and destination memory regions, ensuring the precise transfer of data.

Example:

Let's delve into a concrete illustration to grasp the significance of evaluating wmemmove. Suppose we have a lengthy string, essentially a sentence, and we aim to relocate a segment of it within the same line.

Example

#include <iostream>
#include <cwchar>
 
int main() {
 // Define a wide string
 const wchar_t originalString[] = L"Programming is fascinating.";
 
 // Display the original string
 wprintf(L"Original String: %ls\n", originalString);
 
 // Specify the number of wide characters to move
 size_t count = 10;
 
 // Create a destination array with enough space
 wchar_t destinationString[30];
 
 // Use wmemmove() to move the specified number of characters
 wmemmove(destinationString, originalString + 12, count);
 
 // Null-terminate the destination string
 destinationString[count] = L'\0';
 
 // Display the modified string
 wprintf(L"Modified String: %ls\n", destinationString);
 
 return 0;
}

Output:

Output

Original String: Programming is fascinating.
Modified String: fascinating.

Breaking Down the Code:

Include Necessary Headers:

Start with #include,<iostream> , to include input and output functionalities; and #include,<cwchar> to include standard input and output operations.

Declare the Main Function:

Define the fundamental purpose and the starting point for our software application.

Initialize the Original String:

Declare an array with a lengthy string name originalString and assign it the value of the sentence "coding is fascinating".

Display the Original String:

Display the provided string using the wprintf function.

Specify the Number of Characters to Move:

Now, assign the variable count to the total count of wide characters that are going to be shifted.

Create a Destination Array:

Allocate sufficient space in the destinationString variable for accommodating the characters that have been shifted.

Utilize wmemmove:

The essential process involves utilizing remove (originalString + 12), which transfers a designated character position from the initial string to the target array.

Null-Terminate the Destination String:

Ensure proper handling only if the target string has been terminated with a null character sequentially.

Display the Modified String:

Output the updated string using wprintf .

Example 1:

Example

#include <iostream>
#include <cwchar>
 
int main() {
 // Define a wide string
 const wchar_t originalString[] = L"C++ is powerful.";
 
 // Display the original string
 wprintf(L"Original String: %ls\n", originalString);
 
 // Specify the number of wide characters to move
 size_t count = 6;
 
 // Create a destination array with enough space
 wchar_t destinationString[20];
 
 // Use wmemmove() to shift characters to the right
 wmemmove(destinationString + count, originalString, wcslen(originalString) + 1);
 
 // Display the modified string
 wprintf(L"Modified String: %ls\n", destinationString);
 
 return 0;
}

Output:

Output

Original String: C++ is powerful.
Modified String: is powerful.C++

Breaking Down the Code:

Header Inclusions:

The software contains a pair of common C++ headers: Iostream for managing input and output processes, CWChar for handling wide character functionalities.

Main Function:

The main function serves as the starting point when the executable file initiates, as explained earlier in the preceding sections.

Original String Definition:

There exists a wide category of object known as originalString containing the input "C++ is powerful".

Display Original String:

The Wprintf function displays the original string on the console.

Number of Characters to Move:

Six broad characters are moved by modifying the variable count to 6.

Destination Array Creation:

The characters that have been shifted are saved in an array named destinationString, which has been allocated a length of 20 characters to ensure it can hold all the characters.

wmemmove Function Usage:

It employs the wmemmove function to shift characters to the left within the target array. The counting of positions commences from the destination, representing the copied string, towards the source, denoting the original string.

Display Modified String:

The function Wprintf displays the updated string, now containing the shifted characters, on the console.

Program Termination:

It signals that the program has concluded successfully by executing a return zero statement.

Example 2:

Example

#include <iostream>
#include <cwchar>
 
int main() {
 // Define a wide string
 const wchar_t originalString[] = L"Memory overlap is handled by wmemmove.";
 
 // Display the original string
 wprintf(L"Original String: %ls\n", originalString);
 
 // Specify the number of wide characters to move
 size_t count = 10;
 
 // Create a destination array with enough space
 wchar_t destinationString[50];
 
 // Use wmemmove() to move characters with overlapping source and destination
 wmemmove(destinationString + 5, originalString + 10, count);
 
 // Null-terminate the destination string
 destinationString[5 + count] = L'\0';
 
 // Display the modified string
 wprintf(L"Modified String: %ls\n", destinationString);
 
 return 0;
}

Output:

Output

Original String: Memory overlap is handled by wmemmove.
Modified String: overlap is ioverlap.

Breaking Down the Code:

Header Inclusions:

The software incorporates a pair of typical C++ headers: Input and Output Stream (<iostream>) and functions for wide characters (<cwchar>).

Main Function:

The main function serves as the entry point for a program to start its execution.

Original String Definition:

The string originalString containing the value memory overlap is managed by wmemmove for wide strings.

Display Original String:

The function wprintf displays the initial string on the console.

Number of Characters to Move:

It indicates that the variable count will consist of ten wide characters.

Destination Array Creation:

Afterwards, a destinationString array with sufficient capacity (50 character slots wide) is allocated to store the transferred characters.

wmemmove Function Usage:

The wmemmove function is employed to relocate characters within the target array. Starting from position 10 as the source, characters are transferred to position 5 in the destination array. The function also specifies a count for the total number of characters being shifted.

Null-Termination of Destination String:

Output: "The successful printing of the destination string occurs when it is terminated with a null character."

Display Modified String:

The Wprintf function is utilized to showcase the updated string that includes the relocated characters.

Program Termination:

Return 0; signifies the successful completion of the program.

Example 3:

Example

#include <iostream>
#include <cwchar>
 
int main() {
 // Define a wide string
 const wchar_t originalString[] = L"Handling null-terminated strings.";
 
 // Display the original string
 wprintf(L"Original String: %ls\n", originalString);
 
 // Specify the number of wide characters to move
 size_t count = 9;
 
 // Create a destination array with enough space
 wchar_t destinationString[50];
 
 // Use wmemmove() to move characters
 wmemmove(destinationString, originalString + 7, count);
 
 // Null-terminate the destination string
 destinationString[count] = L'\0';
 
 // Display the modified string
 wprintf(L"Modified String: %ls\n", destinationString);
 
 return 0;
}

Output:

Output

Original String: Handling null-terminated strings.
Modified String: null-term

Breaking Down the Code:

Header Inclusions:

The software contains a pair of typical C++ headers: Input and Output managed by <iostream>, and Wide Characters supported by <cwchar>.

Main Function:

The main function serves as the starting point for the program's execution.

Original String Definition:

We initialize a string named originalString with the text "Managing strings terminated by null characters".

Display Original String:

The wprintf function is responsible for outputting the original string onto the console.

Number of Characters to Move:

It will move nine (wide) characters or WCHARS .

Destination Array Creation:

The relocated characters are stored in an array named destinationString, which has a sufficient capacity of 50 wide characters.

wmemmove Function Usage:

The wmemmove function relocates characters starting from the 7th position in the source string to the target array. This parameter specifies the number of characters intended for relocation.

Null-Termination of Destination String:

The null character concludes the destination string to ensure correct output when printing.

Display Modified String:

It presents the altered string with the rearranged letters shown on the screen using the wprintf function.

Program Termination:

The statement return 0; signifies that the program has successfully completed execution without encountering any errors.

However, wmemmove offers certain benefits in comparison to memmove. One key advantage is that it eliminates the necessity to specifically handle wide characters. Unlike memmove, wmemmove is an essential tool for individuals focused on internationalization and localization challenges, particularly in scenarios where wide characters are pivotal.

Conclusion:

In the ever-changing realm of C++ programming, the wmemmove function stands as a testament to the efficiency and adaptability of C++. It is clear to us that wmemmove facilitates precise and reliable manipulation of wide characters. The significance of this function to developers handling multilingual text processing lies in its ability to handle overlapping memory areas and operate with wide characters. Developers who possess a comprehensive understanding of wmemmove can enhance their code, guaranteeing seamless data movement within arrays of wide characters and fortifying their proficiency in C++.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience