I was wondering something about array and the 'new' method use for dynamic array's.I have noticed that withing an array, it's element initialization cannot be changed without having to some how delete the array and reconstruct it(so far to my knowledge). But can the same be said about dynamic arrays?. Will dynamic arrays allow you to add element's without deleting the whole array?. and if so then how and why would it work a certain way, or not work at all.
For example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
int* array;
int o;
cin >> o;
array=newint [o];
for(int i =0;i<o;i++)
array[i]=i;
for(int i = 0;i<o;i++)
cout<<array[i]<<endl;
system("pause");
}
This code lets you create a dynamic array, give it a value, assign a value equal to the elements place for each element, and print out all array elements. But my question is, if it is possible to keep the same array 'array' untouched but at the same time expanding it's elements .
no, if you would expand the array or reduce it with existing value, you must create the array with new size before, copy all elements of the old in the new and finally delete the old array. you can use the memcpy() or memmove() functions, an example:
1 2 3 4 5 6 7 8 9 10 11 12
int *array=newint[10]; //array of 10 elements;
for(int i=0;i<10;i++) array[i]=i;
//increase the size of the new array;
int *increased=newint[20];
memcpy((void*)increased,(constvoid*)array,sizeof(int)*10); //copy ONLY the old values (10 and not 20)
//reduce the size of the new array
int *reduced=newint[5];
memcpy((void*)increased,(constvoid*)array,sizeof(int)*5); //copy ONLY the old values that can be contained(5 and not 10);
//finally delete the old array
delete [] array;
However, you can use also the 'vector' template class of C++ for create dinamic array.
new'd arrays cannot be resized unless destroyed and rebuilt. Of course, you can allocate a new array (while maintaining your old one) then copy the values from the old one to the new one. For example:
You've got a memory leak. On line 15, you discard the memory allocated on line 5 by assigning array to another location. Make sure you delete/delete[] the array prior to allocation. However, the way you're doing it erases the contents of the old array. Though, the old old array is still there, you just abandon it.
So therefore anytime I create a new dynamic array with name=new type[elements];
I am actually discarding any previous allocations and creating new one's?
And if so then what is actually going on when ever you first initialize a dynamic array?. Because as far to my knowledge when ever you create a dynamic array, a pointer is created to represent the array, meaning that the pointer hold's more then one point to value, and that is what makes it a dynamic array.
And if so then what is actually going on when ever you first initialize a dynamic array?
In C++11, dynamic arrays can be initialised, but not in pre-C++11.
Factors wrote:
Because as far to my knowledge when ever you create a dynamic array, a pointer is created to represent the array, meaning that the pointer hold more then one point to value and that is what makes it a dynamic array.
It's true that new/new[] returns a pointer to the new data, but when you assign array to that pointer, arrayforgets the old array and points to the new one. Without a pointer pointing to the old array, it'll leak.