c++ - Dynamic array resize function problems -


Note: I know that using only STL vector for a programming class would be easy, in M, Need to write the dynamic array template class because our professor has enjoyed us redrawing the wheel again.

However, I have created a resize function for my dynamic array template class, note that the private member variables are T * arr, unsigned use and unsigned cap.

  template & lt; Class T & gt; Zero Dar ray & lt; T & gt; :: size (unsigned shape) {if (size> caps) {T * temp_arr = new T [size]; For (integer number = 0; count & used; ++ calculation) temp_arr [count] = arr [count]; For (int calculation = use; calculation & lt; size; ++ calculation) temp_arr [count] = t (); Remove [arr] Arr = temp_arr; Cap = size; Used = size; } If (size & lt; cape) {use = size; }}   

Whenever I use this function to increase the size of the array, it works for the first time when I have to use it, but after that, the Visual Studio Line Will trigger the breakpoint on 14 (remove arr ), and if I continue before the break, I finally get the intended failure of the _CrtIsValidHeapPointer (pUserData) . What is the reason, and how can I fix it?

Constructor:

  template & lt; Class T & gt; Darray & lt; T & gt; :: Darray () {use = 0; Hat = 64; Arr = new t [hat]; For (integer number = 0; calculation & lt; cap; ++ calculation) arr [count] = T ();   

}

Assignment operator:

  template & lt; Class T & gt; Darray & lt; T & gt; & Amp; Darray & lt; T & gt; :: operator = (Cant Darray & amp; right_operand) {remove} arr; Arr = new t [right_operand.capacity ()]; Use = right_operand.size (); Hat = right_operand.capacity (); For (integer number = 0; calculation & lt; use; ++ calculation) arr [count] = right_operand [count]; Return * This; }   

Cons:

  template & lt; Class T & gt; Darray & lt; T & gt; :: ~ Darray () {remove] arr; }   

Some requests have been made for the push_back function, so here also it is:

  template & lt; Class T & gt; Zero Dar ray & lt; T & gt; :: push_back (console and inclusive) {if ((1)> cap} {resize (cap * 2); Arr [used 1] = input; ++ used; } And {arr [used] = input; ++ used; }}    

Your resize function increases Used .

You should add another function, which is similar, when you can enter arr [used + 1] in push_back . To change resize , but only controls the capabilities of your array and the stored object is not counted. (I.e. this is used does not increase) This function should be called by push_back . (As you mentioned std :: vector in your question, and see the difference between.)

In addition: Array index is zero-based. Do not include on the used location on the condition .

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -