0

I have a struct which contains a flexible member array:

typedef struct bar {
  uint8_t  bar_a;
  uint16_t bar_b;
} bar_t;

typedef struct foo {
  uint16_t foo_a;
  uint_8_t n_bars;
  bar_t    *bars[];
} foo_t;

I'm trying to use foo and bar as data structures to copy data over to uint8_t buffer[50].

#define NUMBER_OF_BARS 1;
static uint8_t buffer[50];

static foo_t *
copy_to_buf(void *buf)
{
  int i;
  foo_t *foo = (foo_t *)buf;
  bar_t bar;

  /* Set foo_a */
  foo->foo_a = 1;
  foo->n_bars = NUMBER_OF_BARS;
  /* Set bars */
  for(i=0; i < foo->n_bars; i++) {
    bar.bar_a = 0xFF;
    bar.bar_b = 1234;
    memcpy(&foo->bars[i], &bar, sizeof(bar_t));
  }
  return foo;
}

int main()
{
  int i;
  foo_t *foo = copy_to_buf(&buffer);
  printf("foo ... [%p %p %p %p]\n", 
         &foo, &foo->foo_a, &foo->n_bars &foo->bars);
  for(i=0; i < foo->n_bars; i++) {
    printf("bar ... [%p %p %p]\n", 
           &foo->bars[i], &foo->bars[i]->bar_a, &foo->bars[i]->bar_b);
  }
  return 0;
}

Now I would have expected this to print the following (i.e. a contiguous block of memory):

foo ... [0x1f92 0x1f92 0x1f94 0x1f95]
bar ... [0x1f95 0x1f95 0x1f96]

Instead, I'm getting the following:

foo ... [0x1f92 0x1f92 0x1f94 0x1f95]
bar ... [0x1f95 0x0 0x1]

What am I doing wrong?

4

1 回答 1

2

由于foo.bars是一个指针数组,因此您需要为它们指向的对象分配内存,然后复制到该内存。

static foo_t *
copy_to_buf(void *buf)
{
  int i;
  foo_t *foo = (foo_t *)buf;
  bar_t bar;

  /* Set foo_a */
  foo->foo_a = 1;
  foo->n_bars = NUMBER_OF_BARS;
  /* Set bars */
  for(i=0; i < foo->n_bars; i++) {
    bar.bar_a = 0xFF;
    bar.bar_b = 1234;
    foo->bars[i] = malloc(sizeof(bar_t));
    memcpy(foo->bars[i], &bar, sizeof(bar_t));
  }
  return foo;
}

或者您可以将其声明为结构数组而不是指针。

typedef struct foo {
  uint16_t foo_a;
  uint_8_t n_bars;
  bar_t    bars[];
} foo_t;

并更改您访问它们的方式main()

int main()
{
  int i;
  foo_t *foo = copy_to_buf(&buffer);
  printf("foo ... [%p %p %p %p]\n", 
         &foo, &foo->foo_a, &foo->n_bars &foo->bars);
  for(i=0; i < foo->n_bars; i++) {
    printf("bar ... [%p %p %p]\n", 
           &foo->bars[i], &foo->bars[i].bar_a, &foo->bars[i].bar_b);
  }
  return 0;
}
于 2017-12-07T17:43:59.287 回答