std::memcpy
STD::Memcpy
Defined in header | | |
---|---|---|
void* memcpy( void* dest, const void* src, std::size_t count | | |
复制件count
指向的对象的字节。src
指向的对象dest
两个对象都被重新解释为unsigned char
...
如果对象重叠,则行为未定义。
如果dest
或src
为空指针,则行为未定义,即使count
是零。
如果对象不是TriviallyCopyable
的行为memcpy
未指定,并且可能没有定义...
参数
dest | - | pointer to the memory location to copy to |
---|---|---|
src | - | pointer to the memory location to copy from |
count | - | number of bytes to copy |
返回值
dest
...
注记
std::memcpy
是内存到内存复制最快的库例程。它通常比std::strcpy
,它必须扫描它复制的数据,或者std::memmove
,它必须采取预防措施来处理重叠的输入。
几个C++编译器将适当的内存复制循环转换为std::memcpy
打电话。
何地严格混叠禁止检查作为两种不同类型的值的相同内存,std::memcpy
可用于转换值。
例
二次
#include <iostream>
#include <cstdint>
#include <cstring>
int main()
{
// simple usage
char source[] = "once upon a midnight dreary...", dest[4];
std::memcpy(dest, source, sizeof dest
for (char c : dest)
std::cout << c << '\n';
// reinterpreting
double d = 0.1;
// std::int64_t n = *reinterpret_cast<std::int64_t*>(&d // aliasing violation
std::int64_t n;
std::memcpy(&n, &d, sizeof d // OK
std::cout << std::hexfloat << d << " is " << std::hex << n
<< " as an std::int64_t\n";
}
二次
产出:
二次
o
n
c
e
0x1.999999999999ap-4 is 3fb999999999999a as an std::int64_t
二次
另见
memmove | moves one buffer to another (function) |
---|---|
memset | fills a buffer with a character (function) |
wmemcpy | copies a certain amount of wide characters between two non-overlapping arrays (function) |
copycopy_if (C++11) | copies a range of elements to a new location (function template) |
copy_backward | copies a range of elements in backwards order (function template) |
is_trivially_copyable (C++11) | checks if a type is trivially copyable (class template) |
C.Memcpy文档
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。