1

I use .data() to get 16 bytes data array.
Later I write it to a file and I want to load it back to a uuid variable. Should I just perform memory copy to the variable as : (c++11)

boost::uuids::uuid uuid = boost::uuids::random_generator()();
char[16] data;
std::copy_n(&uuid, 16, data); // copy to data
std::copy_n(data, 16, &uuid); // copy from data (?)
SagiLow
  • 5,721
  • 9
  • 60
  • 115
  • Can you please show more code, preferably a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve)? For example, it would help a lot if we knew what `data` and `uuid` is (we can *guess* but we don't actually *know*). – Some programmer dude Jun 29 '15 at 08:37
  • @JoachimPileborg, i've added more specific example – SagiLow Jun 29 '15 at 08:44

1 Answers1

3

First, whenever you find yourself wondering how to use Boost classes, there's the docs:

http://www.boost.org/doc/libs/1_58_0/libs/uuid/uuid.html

{ // example using memcpy
    unsigned char uuid_data[16];
    // fill uuid_data

    boost::uuids::uuid u;

    memcpy(&u, uuid_data, 16);
}

{ // example using aggregate initializers
    boost::uuids::uuid u =
    { 0x12 ,0x34, 0x56, 0x78
    , 0x90, 0xab
    , 0xcd, 0xef
    , 0x12, 0x34
    , 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef
    };
}

Since memcpy works I expect copy_n will work also.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131