0

Found this answer here: sizeof a struct member

Copied it completely into my code, but my compiler objects that I have a pointer to an incomplete class type.

uint8_t clStructCount = sizeof(((struct ALMConfStr *) 0)->IntelRecsPerPg);

What am I doing wrong? I want to set clStructCount equal to the value of IntelRecsPerPg at runtime; I thought this was the trick to do so.

Thanks!

Community
  • 1
  • 1
nobby
  • 373
  • 1
  • 3
  • 15

3 Answers3

3

The definition of your structure needs to be visible at the point where the compiler encounters your sizeof code.

So, this translation unit should work:

struct ALMConfStr {
  int IntelRecsPerPg[MagicNumber];
};
// ...
uint8_t clStructCount = sizeof(((struct ALMConfStr *) 0)->IntelRecsPerPg);

whether it's all in one file, or the struct is in a header #included before your sizeof code.

However, this:

struct ALMConfStr;
// ...
uint8_t clStructCount = sizeof(((struct ALMConfStr *) 0)->IntelRecsPerPg);

won't work, because the compiler doesn't know what a struct ALMConfStr consists of, or what an IntelRecsPerPg might be in that context.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • Thanks for the reply, however, my struct ALMConfStr is absolutely defined in the header before I try to compute clStructCount. Any other ideas? – nobby Jan 29 '13 at 19:04
  • From my header file: typedef struct ALMConfStruct { uint8_t updateMode; uint8_t flashPgSize; uint8_t flashMaxPagesSize; uint8_t IntelRecsPerPg; fptr_t appResetAddr; uint16_t appEndAddr; uint8_t comIntf; }ALMConfStr; – nobby Jan 29 '13 at 19:05
  • Edit, sorry, nevermind, I was referencing the specific instance and not the struct type. – nobby Jan 29 '13 at 19:07
  • EDIT #2: Still not working (though now it compiles). I get a size of 1, when the value of IntelRecsPerPg is 8. – nobby Jan 29 '13 at 19:19
  • @nobby `IntelRecsPerPg` is a `uint8_t`, that's one byte, so a size of 1. – Daniel Fischer Jan 29 '13 at 19:22
  • Because the example in the link I posted referenced an array, I thought this worked to take the value of the struct element. In fact, it takes the size of the member array. So what if my struct member is a uint8_t and I want to set an array size at runtime to equal the VALUE stored in the uint8_t? – nobby Jan 29 '13 at 19:52
  • Then you'd use `uint8_t clStructCount = conf->IntelRecsPerPg;` (ie, get the value rather than the size of the member), and then _declare an array of clStructCount elements_ – Useless Jan 30 '13 at 13:07
0

I think there is no definition of struct ALMConfStr

or IntelRecsPerPg is not a member of struct ALMConfStr

Say

struct ALMConfStr{
//some member variables
data_type IntelRecsPerPg;
//some more member variables
};

Basically sizeof(((struct ALMConfStr *) 0)->IntelRecsPerPg) tries to get the

sizeof(IntelRecsPerPg)

in the structure assuming that the structure is available at address 0.

sr01853
  • 6,043
  • 1
  • 19
  • 39
0

From a comment on Useless' answer:

From my header file:

    typedef struct ALMConfStruct {
        uint8_t updateMode;
        uint8_t flashPgSize;
        uint8_t flashMaxPagesSize;
        uint8_t IntelRecsPerPg;
        fptr_t appResetAddr;
        uint16_t appEndAddr;
        uint8_t comIntf;
    }ALMConfStr; 

Your struct tag is ALMConfStruct and not ALMConfStr, so

struct ALMConfStr

declares a new incomplete struct type. Remove the struct, or use struct ALMConfStruct in your code to determine the size.

uint8_t clStructCount = sizeof(((ALMConfStr *) 0)->IntelRecsPerPg);

or

uint8_t clStructCount = sizeof(((struct ALMConfStruct *) 0)->IntelRecsPerPg);
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431