How to create a structure with two variable sized

2019-08-14 03:18发布

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

标签: c arrays struct
5条回答
老娘就宠你
2楼-- · 2019-08-14 03:53

In order to serialize variably-sized data, you have to have a boundary tag of some sort. The boundary tag can be either a size written right before the data, or it can be a special value that is not allowed to appear in the data stream and is written right after the data.

Which you choose depends on how much data you are storing, and if you are optimizing for size in the output stream. It is often easier to store a size before-hand, because you know how big to make the receiving buffer. If you don't then you have to gradually resize your buffer on load.

查看更多
老娘就宠你
3楼-- · 2019-08-14 03:59
typedef struct MyStruct_s
{
  int variable_one_size;
  void* variable_one_buf;
  int variable_two_size;
  void* variable_two_buf;
} MyStruct;

MyStruct* CreateMyStruct (int size_one, int size_two)
{
  MyStruct* s = (MyStruct*)malloc (sizeof (MyStruct));
  s->variable_one_size = size_one;
  s->variable_one_buf = malloc (size_one);
  s->variable_two_size = size_two;
  s->variable_two_buf = malloc (size_two);
}

void FreeMyStruct (MyStruct* s)
{
  free (s->variable_one_buf);
  free (s->variable_two_buf);
  free (s);
}
查看更多
我想做一个坏孩纸
4楼-- · 2019-08-14 04:01

In some ways, I'd do things like Dan Olson. However:

1) I'd create the final struct by having two instances of a simpler struct that has just one variable array.

2) I'd declare the array with byte* and use size_t for its length.

Having said this, I'm still not entirely clear on what you're trying to do.

edit

If you want it contiguous in memory, just define a struct with two lengths. Then allocate a block big enough for both blocks that you want to pass, plus the struct itself. Set the two lengths and copy the two blocks immediately after. I think it should be clear how the lengths suffice to make the struct self-describing.

查看更多
做自己的国王
5楼-- · 2019-08-14 04:05

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:

typedef struct _serial {
  size_t sz_a;
  size_t sz_b;
  char data[1]; // "dummy" array as pointer to space at end of the struct
} serial;

serial* malloc_serial(size_t a, size_t b) {
  serial *result;
  // malloc more memory than just sizeof(serial), so that there
  // is enough space "in" the data member for both of the variable arrays
  result = malloc(sizeof(serial) - 1 + a + b);
  if (result) {
    result->sz_a = a;
    result->sz_b = b;
  }
  return result;
}

// access the "arrays" in the struct:

char* access_a(serial *s) {
  return &s->data[0];
}

char* access_b(serial *s) {
  return &s->data[s->sz_a];
}

Then you could do things like this:

  serial *s = ...;
  memcpy(access_a(s), "hallo", 6);
  access_a(s)[1] = 'e';

Also note that you can't just assign one serial to another one, you need to make sure that the sizes are compatible and copy the data manually.

查看更多
你好瞎i
6楼-- · 2019-08-14 04:09

This resolves to something like

typedef struct
{
   size_t arr_size_1, arr_size_2;
   char arr_1[0/*arr_size_1 + arr_size_2*/];
} ...;

The size(s) should be in the front of the dynamic sized data, so that it doesn't move when expanding your array.

You cannot have 2 unknown sized arrays in your struct, so you must collapse them into one and then access the data relative from the first pointer.

查看更多
登录 后发表回答