title: C++ : Copy Array of Structure and return the array of structure
body: <p>I am using Qt Creator to develop an application.</p>
  
  <p>I am not a good C++ programmer, so there might be conceptual errors etc.</p>
  
  <p>I am facing an issue while copying the array of structure and return the structure.</p>
  
  <p>There are ample of solutions related to similar title but could'nt solved my problem.</p>
  
  <p>Here's my code:</p>
  
  <p><strong>ABC.h</strong></p>
  
  <pre><code>typedef struct command_packet_struct
  {
      QString ItemDataName;
      DATA_TYPE ItemDataType;
      int ItemDataLength;
  } COMMAND_PACKET_STRUCT;
  
  class ABC 
  {
  private:
  
      COMMAND_PACKET_STRUCT* getHeaderTitle(int index);
      COMMAND_PACKET_STRUCT *_packetStruct ;
  };
  </code></pre>
  
  <p><strong>ABC.cpp</strong></p>
  
  <pre><code>COMMAND_PACKET_STRUCT _OA_Packet [] = {
      {"ERROR\nMAIN CODE" ,       DATA_TYPE_NUM,          5 },
      {"ERROR\nSUB CODE"  ,       DATA_TYPE_NUM,          4 },
      {"SCALE VERSION"    ,       DATA_TYPE_ASCII,        8 },
      {"E-PRE VERSION"    ,       DATA_TYPE_ASCII,        8 }
  };
  
  COMMAND_PACKET_STRUCT* PCU_SIM_ORDER_TASK::getHeaderTitle(int index)
  {
      int maxSize;
  
      if (_packetStruct != NULL)
      {
          _packetStruct = 0;
      }
  
      _packetStruct = new COMMAND_PACKET_STRUCT(*_OA_Packet);
      maxSize = ((sizeof(_OA_Packet)/sizeof(COMMAND_PACKET_STRUCT)) - 1);
      memcpy(_packetStruct, _OA_Packet, maxSize) ;
  
      qDebug() &lt;&lt; sizeof(COMMAND_PACKET_STRUCT);       // return 12
      qDebug() &lt;&lt; sizeof(_packetStruct);               // return 4
      qDebug() &lt;&lt; sizeof(_OA_Packet);                  // return 48
  
      return _packetStruct;
  }
  </code></pre>
  
  <p>The above code shows the implementation  i have done for Array of structures.</p>
  
  <p>I need to copy array of structure  </p>
  
  <pre><code>           _OA_Packet
  </code></pre>
  
  <p>to </p>
  
  <pre><code>           _packetStruct
  </code></pre>
  
  <p>but when I try to get the size of the <strong>_packetStruct</strong>, it gives <strong>4</strong> while the size of <strong>_OA_Packet</strong> gives <strong>48</strong></p>
  
  <p>Thus when try to display the contents of the structure, it returns with <strong>"segmentation fault error"</strong></p>
  
  <p>Any Idea, Suggestion ??</p>