[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] PROGMEM on compound-literal arrays

From: Paul \"LeoNerd\" Evans
Subject: [avr-gcc-list] PROGMEM on compound-literal arrays
Date: Sun, 30 Oct 2016 11:43:38 +0000

((TLDR: PROGMEM on compound-literal arrays is silently ignored.
  Please either implement or make it a noisy warning/error.))

If I define a function that takes a byte array, e.g.

  void i2c_write(size_t len, const uint8_t *data);

I find it nice to be able to call that with compound-literal arrays

  i2c_write(4, (const uint8_t[]){0x10, 0x20, 0x30, 0x40});

((The reason I'm using this inline notation rather than a static
  declaration made elsewhere is that my data in fact comes from a
  build-time translation step, and most of the arguments to the function
  call are embedded with an #include directive to embed the data in the
  source code.))

If I want to store the data in PROGMEM instead and write a _P version,
then this syntax won't do:

  i2c_write_P(4, (const PROGMEM uint8_t[]){0x10, 0x20, 0x30, 0x40});

While it does parse and compile successfully, the data doesn't end in
in PROGMEM, so the pgm_read_byte() function doesn't find it, and
returns junk. There's no compile-time warning or error that it hasn't
honoured it though, so debugging this failure can be hard.

The following syntax does work as an alternative:

  i2c_write_P(4, ({
    static const PROGMEM uint8_t data[] = {0x10, 0x20, 0x30, 0x40};

I.e. a statement-expression that embeds a static PROGMEM declaration
and yields it.

Would it be possible to make the former syntax work though?

Paul "LeoNerd" Evans

address@hidden      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Attachment: pgpAeJRUVp3F0.pgp
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]