[C/C++] std::memcpy Api Reference

Posted by [하늘이]
2016. 10. 21. 16:10 IT/C, C++
반응형


std::memcpy

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

Defined in header <cstring>

Parameters

dest-pointer to the memory location to copy to
src-pointer to the memory location to copy from
count-number of bytes to copy

Return value

dest


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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";
}
cs

Result

o
n
c
e
0x1.999999999999ap-4 is 3fb999999999999a as an std::int64_t


Ref : 


반응형