I am used to java and php and now I need to write some c++ code. I got difficulties to create a BYTE-array with dynamic size. How to achieve this?
int byteSize = shm.getMemorySize();
BYTE byte[44]; // replace 44 by byteSize
You should use std::vector
unless you have very specific reason to use arrays. Normally, in the similar context where you were using arrays in other languages in C++ the default choice should be std::vector
.
Never use a naked pointer or it is open door for bugs and memory leaks, instead, here some alternatives :
int len = something;
std::vector<char> buffer(len,0);
or c++11 smart pointer
std::unique_ptr<char[]> buffer{ new char[len] };
or c++14 with make_unique
auto buffen = std::make_unique<char[]>(len);
Use a vector, unless you absolutely need to handle memory yourself. Also, this is more of a preference thing, but I prefer to use uint8_t instead of BYTE. Its a bit more compliant as it doesn't depend on GCC.
#include <vector>
#include <cstdint>
...
std::vector<uint8_t> foo;
or
#include <cstdint>
...
uint8_t* data;
data = new uint8_t[10];
...
delete[] data;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With