What is equivalent mmap
into C++?
I have code like below
LCDdata = mmap(NULL, iFrameBufferSize, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fb_fd, 0);
Where LCDdata
is unsigned char
type pointer
, iFrameBufferSize
is int
type and fb_fd
is static int
type.
When I compile it by use of arm g++
tool chain it give me error as below
error: invalid conversion from 'void*' to 'unsigned char*' [-fpermissive]
So how can I use any equivalent type function instead of mmap
?
Which header file I should include? And how this new line's syntax will become?
The C++ equivalent is auto LCDdata = static_cast<unsigned char*>(mmap(...
In C++ we prefer to define out variables only when we initialize them, and because of that we often don't need to specify the type anymore. Here, we don't need to repeat unsigned char*
.
C allows void*
to be assigned to pointers of any type without a cast. C++ does not. C programmers have complained about this for ages ( malloc
being the most common complaint), but it's not likely to change.
The solution is to add the cast. If the source needs to be compilable as C, use a C-style cast:
LCDdata = (unsigned char*)mmap(...);
Otherwise use a C++ cast
LCDdata = static_cast<unsigned char*>(mmap(...));
If you want to do something more drastic, you could look into Boost Interprocess . That would give you RAII.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.