Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using memcpy in C++

Tags:

c++

arrays

memcpy

I am little confused on the parameters for the memcpy function. If I have

int* arr = new int[5];

int* newarr = new int[6];

and I want to copy the elements in arr into newarr using memcopy,

memcpy(parameter, parameter, parameter)

How do I do this?

like image 905
IkeJoka Avatar asked Oct 18 '13 00:10

IkeJoka


People also ask

What is the use of memcpy in C?

(Copy Memory Block) In the C Programming Language, the memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. It returns a pointer to the destination.

What is memcpy and explain the syntax?

memcpy() function is an inbuilt function in C++ STL, which is defined in <cstring> header file. memcpy() function is used to copy blocks of memory. This function is used to copy the number of values from one memory location to another. The result of the function is a binary copy of the data.

In which library is memcpy defined?

C library function - memcpy() The C library function void *memcpy(void *dest, const void *src, size_t n) copies n characters from memory area src to memory area dest.

Is there a copy function in C?

C strcpy() The strcpy() function copies the string pointed by source (including the null character) to the destination. The strcpy() function also returns the copied string.

How to use memcpy () function in C language?

The C library function void *memcpy (void *dest, const void *src, size_t n) copies n characters from memory area src to memory area dest. Following is the declaration for memcpy () function. dest − This is pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

What is the difference between memcpy () and memmove () in C?

memcpy () is used to copy a block of memory from a location to another. It is declared in string.h Below is a sample C program to show working of memcpy (). 2) memcpy () leads to problems when source and destination addresses overlap. memmove () is another library function that handles overlapping well. Writing code in comment?

What is the declaration for memcpy () function?

Following is the declaration for memcpy () function. dest − This is pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

What is the difference between memcpy () and SRC ()?

Following is the declaration for memcpy () function. dest − This is pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*. src − This is pointer to the source of data to be copied, type-casted to a pointer of type void*.


1 Answers

So the order is memcpy(destination, source, number_of_bytes).

Therefore, you can place the old data at the beginning of newarr with

memcpy(newarr, arr, 5 * sizeof *arr);
/* sizeof *arr == sizeof arr[0]  == sizeof (int) */

or at the end with

memcpy(newarr+1, arr, 5 * sizeof *arr);

Because you know the data type of arr and newarr, pointer arithmetic works. But inside memcpy it doesn't know the type, so it needs to know the number of bytes.

Another alternative is std::copy or std::copy_n.

std::copy_n(arr, 5, newarr);

For fundamental types like int, the bitwise copy done by memcpy will work fine. For actual class instances, you need to use std::copy (or copy_n) so that the class's customized assignment operator will be used.

like image 129
Ben Voigt Avatar answered Sep 19 '22 17:09

Ben Voigt