Dynamic Arrays.

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>
using namespace std;
int main()
{
int* array;
int o;
cin >> o;
array=new int [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=new int[10]; //array of 10 elements;
for(int i=0;i<10;i++) array[i]=i;
//increase the size of the new array;
int *increased=new int[20];
memcpy((void*)increased,(const void*)array,sizeof(int)*10); //copy ONLY the old values (10 and not 20)

//reduce the size of the new array
int *reduced=new int[5];
memcpy((void*)increased,(const void*)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.
Last edited on
what library is memcpy in?
string.h
closed account (zb0S216C)
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    int *Old_array(new int[4]);
    for(int I(0); I < 4; ++I)
        Old_array[I] = (I + 1);

    {
        // Resize.
        int *New_array(new int[5]);

        // Copy over.
        for(int I(0); I < 5; ++I)
            New_array[I] = ((I == 4) ? 0 : Old_array[I]);

        // We no longer need the old array.
        delete [ ] Old_array;
        Old_array = New_array;
    }

    for(int I(0); I < 5; ++I)
        std::cout << Old_array[I] << " ";

    delete [ ] Old_array;
}

Although, std::vector doesn't implement its storage in this way.

ui uiho wrote:
what library is memcpy in?


Vins3Xtreme wrote:
string.h

That should be <cstring>. This is C++.

Wazzak
Last edited on
Thanks so far, but im wondering if that is the case then isn't it easyer to just give the dynamic array a new meaning

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
int* array;
int o,c;
cin >> o;
array=new int [o];
for(int i =0;i<o;i++)
array[i]=i;
for(int i = 0;i<o;i++)
cout<<array[i]<<endl;

cin>>c;
array=new int[c];
for(int i =o-1;i<c;i++)
array[i]=i;
for(int i = 0;i<c;i++)
cout<<array[i]<<endl;


system("pause");
}


or is this method invalid in some order or way. explain is so.

Every bit of information is appreciated there for anything written as a respond will be studied carefully by myself. thank you.
Last edited on
closed account (zb0S216C)
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.

Wazzak
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.
Last edited on
closed account (zb0S216C)
Yes. That results in a leak. Prior to another allocation you should delete/delete[] the memory previously allocated. For example:

1
2
3
4
5
6
7
8
int *Array(new int[3]);
delete [ ] Array;

Array = new int[4];

//...

delete [ ] Array;


Factors wrote:
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, array forgets the old array and points to the new one. Without a pointer pointing to the old array, it'll leak.

Wazzak
Last edited on
Topic archived. No new replies allowed.