alloc a struct with zero length array using new
In C (using gcc) I can declare a variable length struct as below:
typedef struct ProtocolFrame
{
uint8_t op;
uint32_t address;
uint16_t size;
uint8_t payload[0];
} ProtocolFrame;
then I can alloc different frame:
开发者_开发问答ProtocolFrame *frA;
ProtocolFrame *frB;
frA = malloc(sizeof(ProtocolFrame) + 50);
frB = malloc(sizeof(ProtocolFrame));
In this example frA has a payload field as big as 50 bytes, and frB has no payload
Can I do the same thing in C++ using the new operator?
template<size_t s>
struct ProtocolFrame
{
uint8_t op;
uint32_t address;
uint16_t size;
uint8_t payload[s];
} ProtocolFrame;
// specialize for no payload
template<>
struct ProtocolFrame<0>
{
uint8_t op;
uint32_t address;
uint16_t size;
} ProtocolFrame;
ProtocolFrame<50> *frA = new ProtocolFrame<50>;
ProtocolFrame<0> *frB = new ProtocolFrame<0>;
To decide what is the size at runtime you could use placement-new operator in cooperation with std::malloc
:
void *buffer = std::malloc(sizeof(ProtocolFrame)+50);
ProtocolFrame *frA = new (buffer) ProtocolFrame;
You can read also this article on the codeproject.com which is contain the full sample.
Use placement new
char *buf = new char[sizeof(ProtocolFrame) + 50]; //pre-allocated buffer
ProtocolFrame *frA = new (buf) ProtocolFrame; //placement new
// STUFF
frA->~ProtocolFrame();
delete [] buf;
when you delete frA it will call ProtocolFrame destructor and free buf allocation
EDIT: I have read that you shouldn't call delete but the destructor directly. I think it might be a compliler specific behaviour. I haven't used placement new much but when I did, I called delete and it worked fine with MSVC++. So the standard correct way seems to be frA->~ProtocolFrame(); and then delete buf; This looks horrible! I suggest you might want to read up on it.
Typically, you would use std::vector.
class ProtocolFrame {
// Invokes undefined behaviour if stuff is not POD.
struct stuff {
stuff(uint8_t lop, uint32_t laddress, uint16_t lsize)
: op(lop), address(laddress), size(lsize) {
}
uint8_t op;
uint32_t address;
uint16_t size;
};
std::vector<uint8_t> payload;
public:
ProtocolFrame(int payloadsize, uint8_t op, uint32_t address, uint16_t size)
: payload(size + sizeof(stuff)) {
new (&payload[0]) stuff(op, address, size);
}
// other methods here
uint32_t GetAddress() {
return ((stuff*)&payload[0])->address;
}
uint16_t GetSize() {
return ((stuff*)&payload[0])->size;
}
uint8_t GetOp() {
return ((stuff*)&payload[0])->op;
}
std::vector<uint8_t>::iterator begin() {
return payload.begin() + sizeof(stuff);
}
std::vector<uint8_t>::iterator end() {
return payload.end();
}
};
This style of code is pretty terrible, though.
In C++ you have classes. Shouldn't the constructor of ProtocolFrame
get a parameter as to how much payload
you want?
struct ProtocolFrame {
uint8_t op;
uint32_t address;
uint16_t size;
uint8_t *payload;
public:
ProtocolFrame (int size) {
payload = new uint8_t [size];
}
~ProtocolFrame () {
delete [] payload;
}
}
Don't know if this is still of interest, but you can overload operator new
to do this. This works with G++ 4.0.1 (I don't know how "nice" it is, feel free to edit and improve it):
#include <cstddef>
template <typename T>
class test {
public:
std::size_t len;
T arr[1];
void *operator new(std::size_t s, std::size_t a);
test(const T& f) { fill(f); }
test();
private:
void fill(const T& f) { for(std::size_t i = 0; i < len; i++) arr[i] = f; }
};
template <typename T>
void *test<T>::operator new(std::size_t s, std::size_t a)
{
void *p = ::operator new(s + (a - 1) * sizeof(T));
// this is bad and we shouldn't do this here blah blah blah
// but I don't know how to pass a to the ctor so this is what I did
((test<T> *)p)->len = a;
return p;
}
The usage is not too horrible either:
#include <iostream>
int main()
{
test<char> *c = new (10) test<char>('c');
std::cout << c->arr[3] << std::endl;
delete c;
return 0;
}
Though resize-in-place may not be possible via new
.
Even in C-style, I think this is barbarious code.
and sizeof() your struct will still return the same size.
why don't you treat payload
as a normal dynamic array ?
EDIT: I do like Kirill V. Lyadvinsky 's answer
EDIT2: @Chris oh I see there are 2 calls to malloc... yes it is less efficient
inline ProtocolFrame * createProtocolFrame(int i)
{
ProtocolFrame * pProto = malloc(sizeof(ProtocolFrame));
pProto->payload = malloc(i * sizeof(uint8_t));
}
精彩评论