c++ - Why mapped_file::data returns char* instead of void* -


or better template <t*>?

in case memory mapped file contains sequence of 32 bit integers, if data() returned void*, able static cast std::uint32_t directly.

why did boost authors choose return char* instead?

edit: pointed out, in case portability issue, translation needed. saying file (or chunk of memory in case) stream of bytes more stream of bits, or of ieee754 doubles, or of complex data structures, seems me broad statement needs more explanation.

even having handle endianness, being able directly map vector of be_uint32_t suggested (and implemented here) make code more readable:

struct be_uint32_t {   std::uint32_t raw;   operator std::uint32_t() { return ntohl(raw); } };  static_assert(sizeof(be_uint32_t)==4, "pod failed"); 

is allowed/advised cast be_uint32_t*? why, or why not?

which kind of cast should used?

edit2: since seems difficult point instead of discussing weather memory model of elaborator made of bits, bytes or words rephrase giving example:

#include <cstdint> #include <memory> #include <vector> #include <iostream> #include <boost/iostreams/device/mapped_file.hpp>  struct entry {   std::uint32_t a;   std::uint64_t b; } __attribute__((packed)); /* compiler specific, supported                                in other ways major compilers */  static_assert(sizeof(entry) == 12, "entry: struct size mismatch"); static_assert(offsetof(entry, a) == 0, "entry: invalid offset a"); static_assert(offsetof(entry, b) == 4, "entry: invalid offset b");  int main(void) {   boost::iostreams::mapped_file_source mmap("map");   assert(mmap.is_open());   const entry* data_begin = reinterpret_cast<const entry*>(mmap.data());   const entry* data_end = data_begin + mmap.size()/sizeof(entry);   for(const entry* ii=data_begin; ii!=data_end; ++ii)     std::cout << std::hex << ii->a << " " << ii->b << std::endl;   return 0; } 

given map file contains bit expected in correct order, there other reason avoid using reinterpret_cast use virtual memory without copying first?

if there not, why force user reinterpret_cast returning typed pointer?

please answer questions bonus points :)

in case memory mapped file contains sequence of 32 bit integers, if data() returned void*, able static cast std::uint32_t directly.

no, not really. still have consider (if nothing else) endianness. "one step conversion" idea lead false sense of security. you're forgetting entire layer of translation between bytes in file , 32-bit integer want program. when translation happens no-op on present system , given file, it's still translation step.

it's better array of bytes (literally char* points to!) know have thinking ensure pointer conversion valid , performing whatever other work required.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -