c++ - Need a better understanding of smart pointers with windows API -
i'm having hard time understanding smart pointers (still in beginning stages of learning tbh). maybe i've been starring @ problem long , i'm missing easy concept...
i'm in process of turning "new/deletes" smart pointers don't have such big issue memory leaks/corruption.
with unique_ptr's can't just:
pchar test; std::unique_ptr<char[]> buffer = std::make_unique<char[]>(10); buffer.get() = test;
(please correct me if i'm wrong) instead, i'm passing raw shared_ptr address of bytes need pe headers. pfilebase have bytes "mz" shared_ptr not coming bytes. missing?
is there way have winapi functions return smart pointer? i'm aware shared_ptr not char[] next step on fixing.
bool initializefromdisk(std::wstring &wstemppath, char *pfilebase) { ... pfilebase = (pchar)mapviewoffile(hfilemapping, file_map_read, 0, 0, 0); if (pfilebase == 0) return false; return true; } int main() { std::shared_ptr<char> pfile = std::make_shared<char>(0); initializefromdisk(l"c:\\...", pfile.get()); ... pimage_dos_signature pdoshdr; std::copy(pfile, 2, pdoshdr); //i'm sure line doesn't quit work yet }
i might this. smart pointers have move constructors, it's pretty efficient return them, , doing yields better code. note use of deleter argument in shared_ptr constructor.
std::shared_ptr<void> initializefromdisk(const std::wstring& wstemppath, char *pfilebase) { ... auto pmappedfile = mapviewoffile(hfilemapping, file_map_read, 0, 0, 0); if (pmappedfile == nullptr) { auto lasterror = getlasterror(); throw system_error(lasterror, system_category()); } return shared_ptr<void>(pmappedfile, [](auto p) { unmapviewoffile(p); }); }
Comments
Post a Comment