I am writing a light weight serialization function and need to include two variable sized arrays within this.
- How should I track the size of each?
- How should I define the struct?
- Am I going about this all wrong?
EDIT: the result must be a contiguous block of memory
Since the data should be continuous in memory it is necessary to malloc a chunk of memory of the right size and manage it’s contents more or less manually. You probably best create a struct that contains the “static” information and related management functions that do the memory management and give access to the “dynamic” members of the struct:
Then you could do things like this:
Also note that you can’t just assign one
serialto another one, you need to make sure that the sizes are compatible and copy the data manually.