GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — C++ cstring memcpy() Function

The C++ Standard Library
  • The C Standard Library
  • C++ Library - Home
  • C++ Library - <fstream>
  • C++ Library - <iomanip>
  • C++ Library - <ios>
  • C++ Library - <iosfwd>
  • C++ Library - <iostream>
  • C++ Library - <istream>
  • C++ Library - <ostream>
  • C++ Library - <sstream>
  • C++ Library - <streambuf>
  • C++ Library - <atomic>
  • C++ Library - <complex>
  • C++ Library - <exception>
  • C++ Library - <functional>
  • C++ Library - <limits>
  • C++ Library - <locale>
  • C++ Library - <memory>
  • C++ Library - <new>
  • C++ Library - <numeric>
  • C++ Library - <regex>
  • C++ Library - <stdexcept>
  • C++ Library - <string>
  • C++ Library - <thread>
  • C++ Library - <tuple>
  • C++ Library - <typeinfo>
  • C++ Library - <utility>
  • C++ Library - <valarray>
  • The C++ STL Library
  • C++ Library - <array>
  • C++ Library - <bitset>
  • C++ Library - <deque>
  • C++ Library - <forward_list>
  • C++ Library - <list>
  • C++ Library - <map>
  • C++ Library - <multimap>
  • C++ Library - <queue>
  • C++ Library - <priority_queue>
  • C++ Library - <set>
  • C++ Library - <multiset >
  • C++ Library - <stack>
  • C++ Library - <unordered_map>
  • C++ Library - <unordered_set>
  • C++ Library - <unordered_multiset>
  • C++ Library - <vector>
  • C++ Library - <algorithm>
  • C++ Library - <iterator>
  • The C++ Advanced Library
  • C++ Library - <any>
  • C++ Library - <barrier>
  • C++ Library - <bit>
  • C++ Library - <chrono>
  • C++ Library - <cinttypes>
  • C++ Library - <clocale>
  • C++ Library - <condition_variable>
  • C++ Library - <coroutine>
  • C++ Library - <cstdlib>
  • C++ Library - <cstring>
  • C++ Library - <cuchar>
  • C++ Library - <charconv>
  • C++ Library - <cfenv>
  • C++ Library - <cmath>
  • C++ Library - <ccomplex>
  • C++ Library - <expected>
  • C++ Library - <format>
  • C++ Library - <future>
  • C++ Library - <flat_set>
  • C++ Library - <flat_map>
  • C++ Library - <filesystem>
  • C++ Library - <generator>
  • C++ Library - <initializer_list>
  • C++ Library - <latch>
  • C++ Library - <memory_resource>
  • C++ Library - <mutex>
  • C++ Library - <mdspan>
  • C++ Library - <optional>
  • C++ Library - <print>
  • C++ Library - <ratio>
  • C++ Library - <scoped_allocator>
  • C++ Library - <semaphore>
  • C++ Library - <source_location>
  • C++ Library - <span>
  • C++ Library - <spanstream>
  • C++ Library - <stacktrace>
  • C++ Library - <stop_token>
  • C++ Library - <syncstream>
  • C++ Library - <system_error>
  • C++ Library - <string_view>
  • C++ Library - <stdatomic>
  • C++ Library - <variant>
  • C++ STL Library Cheat Sheet
  • C++ STL - Cheat Sheet
  • C++ Programming Resources
  • C++ Programming Tutorial
  • C++ Useful Resources
  • C++ Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

C++ cstring memcpy() Function



Previous
Quiz
Next

The memcpy() function in C++, is used for copying data from one memory buffer to another memory buffer. This function is defined in cstring header file.

The function takes three arguments, it does not act like other higher-level functions. Also, it does not check overlapping memory regions and it performs a raw memory copy.

This function does not throw any exception but can cause undefined behavior if the source and destination memory regions are null, overlapping or if size specified is greater than the actual size of the source memory region.

Syntax

Following is the syntax of the C++ cstring memcpy() Function −

void *memcpy(void *destination, const void *source, size_t num);

Parameters

Following are the parameters of this function −

  • destination: Pointer to the destination array where the content is to be copied.

  • source: Pointer to the source of data to be copied.

  • num: Number of bytes to copy.

Return Value

The memcpy() function returns a pointer to the destination, which is the same as the destination pointer passed as the first argument.

Example 1

Let us understand how to use the memcpy() function with the help of an example.

In this example, let's try to copy the content of one array to another array using the memcpy() function.

#include <iostream>
#include <cstring>

using namespace std;
int main() {
   char source[] = "Hello, World!";
   char destination[100];
   
   // Copying the content of source array to destination array
   memcpy(destination, source, strlen(source) + 1);
   
   cout << "Source: " << source << endl;
   cout << "Destination: " << destination << endl;
   
   return 0;
}

Output

When you run the program, the output will be −

Source: Hello, World!
Destination: Hello, World!

Example 2

Let us understand another example of using the memcpy() function.

In this example, we will copy the content of one structure to another structure using the memcpy() function.

#include <iostream>
#include <cstring>

using namespace std;

struct Employee {
   char name[50];
   int age;
   float salary;
};

int main() {
   struct Employee source = {"John Doe", 30, 50000.0};
   struct Employee destination;
   
   // Copying the content of source structure to destination structure
   memcpy(&destination, &source, sizeof(source));
   
   cout << "Source:" << endl;
   cout << "Name: " << source.name << endl;
   cout << "Age: " << source.age << endl;
   cout << "Salary: " << source.salary << endl;
   
   cout << endl;
   
   cout << "Destination:" << endl;
   cout << "Name: " << destination.name << endl;
   cout << "Age: " << destination.age << endl;
   cout << "Salary: " << destination.salary << endl;
   
   return 0;
}

Output

When you run the program, the output will be:

Source:
Name: John Doe
Age: 30
Salary: 50000.00

Destination:
Name: John Doe
Age: 30
Salary: 50000.00

Example 3

In this example, we will try to copy the content of one array to another array using the memcpy() function. Here, we are creating two arrays, one source array and one destination array. We will then try to copy the content of the source array to the destination array using the memcpy() function, but we will pass the size greater than the actual size of the source memory region.

#include <iostream>
#include <cstring>

using namespace std;

int main() {
   char source[] = "Hello, World!";
   char destination[100];
   
   // Copying the content of source array to destination array
   memcpy(destination, source, strlen(source) + 4);
   
   cout << "Source: " << source << endl;
   cout << "Destination: " << destination << endl;
   cout << "Extra bites on destination: " << destination[strlen(source)+2] << endl;
   
   return 0;
}

Output

When you run the program, the output will be:

Source: Hello, World!
Destination: Hello, World!
Extra bites on destination: 

As you can see, the output is not as expected. This is because we passed the size greater than the actual size of the source memory region. This is an exception case of the memcpy() function.

Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.