Dynamic Arrays

Hello all,

I'm trying to create a function that doubles the length of an dynamic array. It does so by creating a dynamic array the size of 2*n, copying all values from the old array into the new one, intitializing all the empty spots as 0 and then overwriting the old one with the new one. Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void vergrossern(int *arr)
{
	int *temp = new int[2*n];
	for	(int i=0;i<2*n;i++)
	{
		if (i<n)
			temp[i] = arr[i];
		else
			temp[i] = 0;
	}
	arr = temp;
	n *=2;
}


int main()
{
	cin >> n;
	nums = new int[n];
	for (int i=0;i<n;i++)
	{
		nums[i] = i;
	}
	vergrossern(nums);
	for (int i=0;i<n;i++)
	{
		cout << nums[i] << " ";
	}
	system("pause");
	return 0;
}


However, it does not initizalise any values bigger than old n to 0. Instead, they come up with weird minus numbers.

What is the problem?
Regards
Try this and see if you can work it out :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;
int n;
int* nums;

void vergrossern(int *arr)
{
	int *temp = new int[2*n];
	for	(int i=0;i<2*n;i++)
	{
		if (i<n)
			temp[i] = arr[i];
		else
			temp[i] = 0;
	}
        cout << "Address of bigger array: " << temp << endl;
	arr = temp;
	n *=2;
}


int main()
{
	cin >> n;
	nums = new int[n];
	for (int i=0;i<n;i++)
	{
		nums[i] = i;
	}
        cout << "address nums BEFORE: " << nums << endl;
	vergrossern(nums);
        cout << "address nums AFTER: " << nums << endl;
	for (int i=0;i<n;i++)
	{
		cout << nums[i] << " ";
	}
	
	return 0;
}
This is actually pretty interesting.

One thing I noticed that you are missing is a delete statement. Without this you have a serious memory leak. I inserted this and found that ALL of the values now appear to be un-initialized. This means that while we are probably changing the arr array successfully, we are failing to change the address pointed to by num. Let's fix this by sending the num pointer by reference instead of making *arr a copy of the pointer. It's so meta!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

void vergrossern(int* &arr, int &n)
{
    int* temp = new int[2*n];

    for (int i=0; i < 2*n; i++)
        if (i<n)  temp[i] = arr[i];
        else      temp[i] = 0;

    delete[] arr;
    arr = temp;
    n*=2;
}

int main()
{
    int n;
    cin >> n;
    int* nums = new int[n];
    for (int i=0;i<n;i++)
        nums[i] = i;

    vergrossern(nums, n);
    for (int i=0;i<n;i++)
        cout << nums[i] << " ";

    return 0;
}


Edit: Awwww, I got ninja'd
Last edited on
Oh and here are some functions in #include <algorithm> that you may find useful:

1
2
3
4
5
6
7
8
9
10
11
void vergrossern(int* &arr, int &n)
{
    int *temp = new int[2*n];

    std::copy(arr, arr+n, temp); // Copies [arr,arr+n) to temp;
    std::fill(temp+n, temp+2*n, 0); // Fills the remaining values with 0;

    delete[] arr;
    arr = temp;
    n*=2;
}
Thanks Moschops for the little exercise, but Stewbond ruined it for me :( Nah joking!

Anyways, it's quite interesting. I'm passing a pointer as argument. That pointer points to an area in the heap. Now, if I pass it through the argument, it gets copied, but the copy of it should still point at the same address. Correct? I was thinking of passing it by reference, but I didn't think it would make a difference.

The other peculiar thing which I don't understand is the initialization of my temp array. I mean no matter what I pass as argument in the feature, it should initialize the last few elements with a 0. If someone could explain to me in detail what's going?

And thanks Stewbond for those useful functions.
copy of it should still point at the same address. Correct?


Yes, the copy points to the same address. And then you change that copy... and then when the function ends the copy is destroyed and you're left with the unchanged original, still pointing at the FIRST array, and not pointing a t the new, bigger array.

If you pass that pointer by reference, when you change it, and the function ends, you will have changed the original.

it should initialize the last few elements with a 0.

It does. You're not looking at them. You're looking at some random bytes off the end of your first, original, small array.
Topic archived. No new replies allowed.