title: C++ Initialization of array in structure
body: <p>I would like initiated size of array in structure when I constructing object:</p>
  
  <pre><code>class someclass{
  public:
    someclass(int init_msgsz) { this-&gt;msgsz = init_msgsz; }
  
  private:
     int msgsz;
     struct msgbuf {
        long msqtype;
        uint8_t msgtext[msgsz];
     } send_message_buf, receive_message_buf;
  };
  </code></pre>
  
  <p>But something is wrong :-( anybody help me?
  <br>Note this structure is from:<i> msgrcv, msgsnd - message operations</i> </p>
  
  <h2>Update:</h2>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/msg.h&gt;
  #include &lt;stdint.h&gt; //uint8_t == unsigned char
  #include &lt;string.h&gt;
  
  class communication
  {
  public:
      communication(key_t &amp;init_key, int &amp;init_msgflg, size_t init_msgsz);
      ~communication();
  
      int send_msg(uint8_t *send_msg);
      int receive_msg(uint8_t *rec_msg);
  
  private:
      int msqid;
      key_t key;
      int msgflg;
      int msgsz;
      size_t buf_length;
  
      struct msgbuf {
          long    msqtype;
          uint8_t    *msgtext;
          } send_message_buf, receive_message_buf;
      int get_msgid();
  };
  
  communication::communication(key_t &amp;init_key, int &amp;init_msgflg, size_t init_msgsz)
  {
      this-&gt;key = init_key;
      this-&gt;msgflg = init_msgflg;
      this-&gt;msgsz = init_msgsz;
      get_msgid();
  
      send_message_buf.msgtext = new uint8_t[this-&gt;msgsz];
      receive_message_buf.msgtext = new uint8_t[this-&gt;msgsz];
  }
  communication::~communication()
  {
      delete [] send_message_buf.msgtext;
      delete [] receive_message_buf.msgtext;
      send_message_buf.msgtext = NULL;
      receive_message_buf.msgtext = NULL;
  }
  
  int communication::get_msgid()
  {
      if ((this-&gt;msqid = msgget(this-&gt;key, this-&gt;msgflg )) &lt; 0)
      {
          std::cout &lt;&lt; "\n[erro]...msgget\n";
          return -1;
      }
      return 0;
  }
  
  int communication::send_msg(uint8_t *send_msg)
  {
      std::copy ( send_msg, send_msg+this-&gt;msgsz,
      this-&gt;send_message_buf.msgtext);
      this-&gt;buf_length = sizeof(this-&gt;send_message_buf.msgtext);
      this-&gt;send_message_buf.msqtype = 1;
  
      if (msgsnd(this-&gt;msqid, &amp;send_message_buf, this-&gt;buf_length, IPC_NOWAIT) &lt; 0)
      {
          std::cout &lt;&lt; "\n[erro]...msgsnd\n";
          return -1;
      }
  
      return 0;
  }
  
  int communication::receive_msg(uint8_t *rec_msg)
  {
      if (msgrcv(this-&gt;msqid, &amp;receive_message_buf, this-&gt;msgsz+1, 1, 0) &lt; 0) 
      {
          std::cout &lt;&lt; "\n[erro]...msgrcv\n";
          return -1;
      }
  
      std::copy ( this-&gt;receive_message_buf.msgtext,       
      this-&gt;receive_message_buf.msgtext+this-&gt;msgsz, rec_msg);
  
      return this-&gt;msgsz;
  }
  
  
  int main()
  {
  
      size_t msgsz = 16;
      int msgflg = IPC_CREAT | 0666;
      key_t keySpi = 1001;
  
      unsigned int msg_size;
      uint8_t receive_msg[msgsz];
      uint8_t send_msg[] = {0xFF, 0xAA, 0xAB, 0xFF, 0xAA, 0xAB, 0xFF, 0xAA, 0xAB, 0xFF, 0xAA, 0xAB, 0xFF, 0xAA, 0xAB, 0xFF};
  
      communication msgSpi(keySpi, msgflg, msgsz);
      msgSpi.send_msg(send_msg);
      msg_size = msgSpi.receive_msg(receive_msg);
  
      std::cout &lt;&lt; "I got msg with size: " &lt;&lt; msg_size &lt;&lt; std::endl;
      for (int i = 0; i &lt; msg_size; i++)
          std::cout &lt;&lt; (int)i &lt;&lt; ": " &lt;&lt; (int)receive_msg[i] &lt;&lt; std::endl;
  
      std::cout &lt;&lt; "\n";
      return 0;
  }
  </code></pre>
  
  <p>Compilation OK, but I can't delete mem in destructor.</p>