title: Allocating Memory to Structures using Operator new and sizeof
body: <p>I understand that this post deals with more than one specific issue which is not ideal but they are related in a real world problem.  It would be a very beneficial learning exercise for me if someone could explain why things are not working.  please appreciate that I am a newbie and trying to work on some legacy C++ console code using VS2019.</p>
  <p>The old code uses Classes that have members that were assigned memory individually using malloc which I understand was for C and I am supposed to use new and delete for C++.  I would like to know why my attempts to assign memory using &quot;operator new&quot; and &quot;sizeof&quot; are not working and if my attempts to simplify the code are overly optimistic.  I have spent a lot of time on this and tried to condense the issues to make the problem clearly coded and commented.  The problem as I see it is essentially:</p>
  <ol>
  <li><p>How can I return the true memory requirements of a structure or class in bytes using sizeof and allocate memory during runtime with user input (I can only return the size of the class/structure pointer as opposed to the dereferenced total number of bytes pointed to)?</p>
  </li>
  <li><p>How can I use multiples of (1) given user input at runtime: i.e. (sizeof(*structure) * x) to properly assign memory?</p>
  </li>
  <li><p>After successfully assigning memory; how can I copy ranges of fields within structures without laboriously looping each member variable: is this possible with fields where the pointers reference different types with varying multiples of bytes?</p>
  </li>
  </ol>
  <p>Thanks for helping!</p>
  <pre><code>#include &lt;iostream&gt;     // std::cout
  #include &lt;new&gt;          // ::operator new
  #include &lt;iostream&gt;     // std::cout
  #include &lt;algorithm&gt;    // std::copy
  #include &lt;vector&gt;       // std::vector
  
  //  define structure &quot;part&quot;
  struct part {
  int* sprocket;
  int* bolt;
  double* cost;
  double* postage;
  double* packing;
  double* insurance;
  };
  // define class &quot;parent&quot;
  class parent {
  public:
  int* phone;
  part* variable;
  };
  
  int main()
  {
  
  using namespace std;
  
  // declare pointers
  parent *father, *mother;
  part *copy;
  
  int size1, size2, size3, size4, size5, size6, size7, size8;
  int i, l;
  
  // get length variable at runtime
  cout &lt;&lt; &quot;enter length&quot; &lt;&lt; endl;
  cin &gt;&gt; l;
  
  // fetch individual pointer variable sizes in bytes
  size1 = sizeof(*father-&gt;variable-&gt;sprocket);    // size of int
  size2 = sizeof(*father-&gt;variable-&gt;bolt);        // size of int
  size3 = sizeof(*father-&gt;variable-&gt;cost);        // size of double
  size4 = sizeof(*father-&gt;variable-&gt;postage);     // size of double
  size5 = sizeof(*father-&gt;variable-&gt;packing);     // size of double
  size6 = sizeof(*father-&gt;variable-&gt;insurance);   // size of double
  size7 = sizeof(*father);                        // size of total struct
  size8 = sizeof(*copy);                          // size of total struct
  // print these to console for checking
  cout &lt;&lt; &quot;sizes in bytes&quot;                         &lt;&lt; endl;
  cout &lt;&lt; &quot;sizeof int sprocket is:&quot;       &lt;&lt; size1 &lt;&lt; endl;
  cout &lt;&lt; &quot;sizeof int bolt is:&quot;           &lt;&lt; size2 &lt;&lt; endl;
  cout &lt;&lt; &quot;sizeof double cost is:&quot;        &lt;&lt; size3 &lt;&lt; endl;
  cout &lt;&lt; &quot;sizeof double postage is:&quot;     &lt;&lt; size4 &lt;&lt; endl;
  cout &lt;&lt; &quot;sizeof double packing is:&quot;     &lt;&lt; size5 &lt;&lt; endl;
  cout &lt;&lt; &quot;sizeof double insurance is:&quot;   &lt;&lt; size6 &lt;&lt; endl;
  cout &lt;&lt; &quot;sizeof 'father' is:&quot;           &lt;&lt; size7 &lt;&lt; endl;
  cout &lt;&lt; &quot;sizeof 'copy' is:&quot;             &lt;&lt; size8 &lt;&lt; endl;
  
  //  This is the output to the console:
  
  //  enter length
  //  10
  //  sizes in bytes
  //  sizeof int sprocket is : 4
  //  sizeof int bolt is : 4
  //  sizeof double cost is : 8
  //  sizeof double postage is : 8
  //  sizeof double packing is : 8
  //  sizeof double insurance is : 8
  //  sizeof 'father' is : 8
  //  sizeof 'copy' is : 24
  
  
  /* cannot work out a way of finding total number of bytes associated with
  structure or class /*
  
  /* I want to avoid doing this!!! */
  
  //father-&gt;phone             = (int*)malloc(sizeof(int) * l);
  //father-&gt;variable-&gt;bolt    = (int*)malloc(sizeof(int) * l);
  //father-&gt;variable-&gt;................... etc etc etc
  
  /* and instead do this */
  
  father = (parent*) :: operator new (sizeof(parent) * 2 * l);
  if (father == NULL)
  cout &lt;&lt; &quot;error assigning memory to variable.&quot; &lt;&lt; endl;
  
  copy = (part*) :: operator new (sizeof(part) * l);
  if (copy == NULL)
  cout &lt;&lt; &quot;error assigning memory to copy.&quot; &lt;&lt; endl;
  
  /*  but cannot seem to get the memory assignment right!!! 
  and get a &quot;write access violation&quot; here which I'm certain 
  is due to the &quot;new&quot; allocation above  */
  
  /* this loop only to populate data in &quot;father&quot; to check memory
  allocation and data for checking copying later*/    
  for (i = 0; i &lt; (2 * l); i++) {
  father-&gt;phone[i]                = i;
  father-&gt;variable-&gt;sprocket[i]   = i;
  father-&gt;variable-&gt;bolt[i]       = i;
  father-&gt;variable-&gt;cost[i]       = i * 0.3; // multiply by 0.3 to give double
  father-&gt;variable-&gt;postage[i]    = i * 0.3; // multiply by 0.3 to give double
  father-&gt;variable-&gt;packing[i]    = i * 0.3; // multiply by 0.3 to give double
  father-&gt;variable-&gt;insurance[i]  = i * 0.3; // multiply by 0.3 to give double
  }
  
  /*  I wish to copy a range (in this example half user input l) from 
  the &quot;part&quot; structure of &quot;father&quot; to the part structure 
  pointed to by &quot;copy&quot; but would like to do it elegantly 
  as follows rather than loop through each 
  member pointer.
          
  I'd like to assign the new pointer &quot;copy&quot; to another &quot;parent&quot; object 
  later in code.  This avoids a lot of complication in further modifying 
  the legacy code hence cannot pass class object as argument 3*/
  
  //std::copy(father-&gt;variable, father-&gt;variable + (l/2), copy.begin());
  
  // later in code.........
  
  //initialize &quot;mother&quot;
  //mother-&gt;variable = copy;
  
  }
  </code></pre>