attribute and struct

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

This came up in another forum where a user happened to place EEMEM between struct and the struct name (by mistake). The variable ended up in SRAM instead of in EEMEM. It seems to be the same for PROGMEM.

This compiles without warnings or errors

#include 
#include 

struct test {
    int i;
    char a[5];
};

struct PROGMEM test fool = {1, "fool"};
struct test PROGMEM bark = {2, "bark"};

But only bark is in PROGMEM, fool is in SRAM

$ egrep 'foo|bar' main.map
                0x000000000000008c                bark
                0x0000000000800100                fool

Does anyone have a good explanation for this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Compiled as C++ gets

warning: attributes ignored on elaborated-type-specifier that is not a forward declaration

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yet another reason to use typedefs, then... :wink:

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But isn't the typedefs deprecated (or soon to be) ?

/Bingo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I usually typedef my struct's, I was just curious of what was going on here..

But compiled as c++ I get these warnings,

main.cc:14: warning: attributes ignored on elaborated-type-specifier that is not a forward declaration
main.cc:15: warning: only initialized variables can be placed into program memory area

So it thinks that fool is just a declaration? (I'm not sure I understand what the warning on line 14 means)
In line 15 it thinks that bark isn't initialized? But it does initialize both structs, but in different memories.

And I get the same (as the last above) warning on all of these

PROGMEM int i = 7;
int PROGMEM j = 7;
int k PROGMEM = 7;

but they are all put in PROGMEM anyway.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Referring to http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#Variable-Attributes the syntax should be

struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };
struct duart b __attribute__ ((section ("DUART_B"))) = { 0 };
char stack[10000] __attribute__ ((section ("STACK"))) = { 0 };
int init_data __attribute__ ((section ("INITDATA")));

contains:

#define PROGMEM __ATTR_PROGMEM__
#define __ATTR_PROGMEM__ __attribute__((__progmem__))

So the correct syntax should be

struct duart a PROGMEM = { 0 };
struct duart b PROGMEM = { 0 };
char stack[10000] PROGMEM = { 0 };
int init_data PROGMEM;

/Martin.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Browse the GCC bugzilla. There are several problems with attribute progmem like PR38346, PR51756, PR49764, PR34734, PR44643 and maybe others.

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bingo600 wrote:
But isn't the typedefs deprecated (or soon to be) ?
As I understand it, attributes on typedefs were never supposed to work.
That they worked at all was a bug.
Unfortunately, things like prog_char got a foothold anyway.

That said, using a typedef to turn a two-token type name into a one-token type name would alleviate the problem under discussion.

"Demons after money.
Whatever happened to the still beating heart of a virgin?
No one has any standards anymore." -- Giles

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

skeeve wrote:
Bingo600 wrote:
But isn't the typedefs deprecated (or soon to be) ?
As I understand it, attributes on typedefs were never supposed to work.
Attributes in typedefs work if they are supposed and implemented accordingly.

The "deprecated" refers solely to "progmem in typedef".

avrfreaks does not support Opera. Profile inactive.