i have a packet struct which have a variable len for a string example:
BYTE StringLen;
String MyString; //String is not a real type, just trying to represent an string of unknown size
My question is how i can make the implementation of this packet inside an struct without knowing the size of members (in this case strings). Here is an example of how i want it to “look like”
void ProcessPacket (PacketStruct* packet)
{
pointer = &packet.MyString;
}
I think its not possible to make since the compiler doesn’t know the size of the string until run time. So how can make it look high level and comprehensible?.
The reason i need structs its for document every packet without the user actually have to look any of the functions that analyze the packet.
So i can resume the question to: is there a way to declare an struct of undefined size members or something close as a struct?
I would recommend a shell class that just interprets the packet data.
As mentioned in comments, you wanted a way to treat a variable-sized packet like a
struct. The old C way to do that was to create astructthat looked like this:And then, cast the data (remember, this is C code):
But, you are entering undefined behavior, since to access the full range of data in
strpack, you would have to read beyond the1byte array boundary defined in thestruct. But, this is a commonly used technique in C.But, in C++, you don’t have to resort to such a hack, because you can define accessor methods to treat the variable length data appropriately.