2

我知道大约有一百万个这样的帖子,但我不知道如何解决这个问题。是不是因为上面定义了 elua_adc_ch_state 然后再次使用?错误的行被标记。

typedef struct 
{
  // Status Bit Flags
  volatile u8     op_pending: 1, // Is there a pending conversion?
                  blocking: 1, // Are we in blocking or non-blocking mode? (0 - blocking, 1 - nonblocking)
                  freerunning: 1, // If true, we don't stop when we've acquired the requested number of samples
                  smooth_ready: 1, // Has smoothing filter warmed up (i.e. smoothlen samples collected)
                  value_fresh: 1; // Whether the value pointed to by value_ptr is fresh

  unsigned        id;

  u8              logsmoothlen;
  volatile u16    smoothidx;
  volatile u32    smoothsum;
  u16             *smoothbuf;

  volatile u16    reqsamples;
  volatile u16    *value_ptr;
} elua_adc_ch_state;

typedef struct
{
  elua_adc_ch_state   *ch_state[ NUM_ADC ]; *** <----
  volatile u16        sample_buf[ NUM_ADC ]; *** <----
  volatile u8         clocked: 1,
                      force_reseq: 1,
                      skip_cycle: 1,
                      running: 1; // Whether or not sequence is running
  volatile u32        ch_active; // bits represent whether channel should be converted on this device
  volatile u32        last_ch_active; // keep copy of old configuration
  unsigned            timer_id, seq_id; // Timer bound to device, sequencer device id
  volatile u8         seq_ctr, seq_len;
} elua_adc_dev_state;

定义并包含所有 u16、u8、NUM_ADC 分配。我只是不确定为什么当第一个结构很好时第二个结构会失败......也没有循环头依赖项。

谢谢

4

1 回答 1

4

最有可能的问题是结构顶部的可变长度数组灵活数组成员,它们不是真正可移植的。VLA 灵活数组成员的规则要求( §6.7.2.1p3)它们位于结构定义的末尾:

typedef struct{
   int foo;
   unsigned int bar;
   int foobar []
} barfoo;

我强烈建议不要使用VLA灵活数组成员,因为它们仅存在于 ANSI C99 中,并且在更广泛实施的 ISO C11 (参见 §6.10.8.3)中是可选的。 ANSI 标准似乎甚至没有指定在 C11 结构中使用 VLA 的行为,因此它可能是未定义的行为。

更新:

根据 Casey 在下面的评论,它们被定义为灵活的数组成员 (第 6.7.2.1p18 节),但是即使在标准中,它们也确实有许多未定义行为的案例(第 6.7.2.1p21 节),应该避免。

于 2013-07-31T20:46:10.003 回答