首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::memcpy

Defined in header <cstring>

void* memcpy( void* dest, const void* src, std::size_t count );

复制件count指向的对象的字节。src指向的对象dest两个对象都被重新解释为unsigned char...

如果对象重叠,则行为未定义。

如果destsrc为空指针,则行为未定义,即使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可用于转换值。

二次

代码语言:javascript
复制
#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";
}

二次

产出:

二次

代码语言:javascript
复制
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文档

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券