K
K
KiVa2018-02-28 08:47:27
C++ / C#
KiVa, 2018-02-28 08:47:27

I came across a strange structure declaration - where can I read about this?

Here's a code snippet:

struct t_EEPROMOffsets
{
  t_EEPROMOffsets(bool isEKM)
    {
      if (isEKM)
        {
          NumSrtucts = 9;
          Size = sizeof(t_EEPROMImage);
        }
      else
        {
          NumSrtucts = 8;
          Size = sizeof(t_EEPROMImage) - sizeof(t_ExtraMul1);
        }
      Offsets = new WORD [NumSrtucts];
      Offsets[0] = offsetof(t_EEPROMImage, stHead1);
      Offsets[1] = offsetof(t_EEPROMImage, stBPPS_Param1);
      Offsets[2] = offsetof(t_EEPROMImage, stAdcGrad1);
      Offsets[3] = offsetof(t_EEPROMImage, stTcorrCoef1);
      Offsets[4] = offsetof(t_EEPROMImage, stKeyClb1);
      Offsets[5] = offsetof(t_EEPROMImage, stItcUst1);
      Offsets[6] = offsetof(t_EEPROMImage, stDACPar1);
      Offsets[7] = offsetof(t_EEPROMImage, stDACGrad1);
      if (isEKM)
        Offsets[8] = offsetof(t_EEPROMImage, stExtraMul1);      
    }
  ~t_EEPROMOffsets()
    {
      delete[] Offsets;
    }
  WORD NumSrtucts;
  WORD Size;
  WORD* Offsets;
};

Working code. I didn't know before that the number of fields inside a struct can be variable ( see IF() ), i.e. It turns out that I lack knowledge. Where it is possible to esteem about such way of the declaration of structures? What are the restrictions? Or who can explain? Interested not in general information, but specific to this case.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wawa, 2018-02-28
@KiVa

1) It's not C, but C++ (look at the question tag)
2) The number of structure fields cannot be variable. There are three of them: NumSrtucts, Size, Offsets.
3) Offsets is a pointer to which the heap-allocated memory is assigned in the constructor. And the size of this memory varies dynamically at the time the constructor is called, but this memory is stored outside the structure, and as it was said in the heap (dynamic memory), the structure only stores the address of this heap section in Offsets.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question