Dynamic arrays and pointers.

This one of the homework questions I have, and I'm having some issues.

I have to write a function that allocates a dynamic array with the size of the variable amount. Send it to a function to fill the array from 1 to the variable amount. Then return it into a unique_ptr and display all the elements of the arary. As far as I can tell everything works in the second function. But I'm having issues with the unique_ptr, was hoping for a nudge in the right direction. I have looked at documentation online about unique_ptr but I really don't understand what I'm doing wrong.

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
 #include <iostream>
#include <memory>
using namespace std;

unsigned *AllocateIntegerArray(unsigned);

int main()
{
	unsigned int amount = 50;
	//AllocateIntegerArray(amount);


	unique_ptr<unsigned>ptr (AllocateIntegerArray(amount));

	for (int count = 1; count < amount; count++)
	{
		cout << ptr[count];
		cout << " ";
	}
}

unsigned int *AllocateIntegerArray(unsigned amount)
{
	unsigned* array = nullptr;
	array = new unsigned[amount];
	int zeroloop = 0;

	for (unsigned loop = 1; loop <= amount; ++loop)
	{
		array[zeroloop] = loop;
		zeroloop++;

	}

	return(array);
}
http://www.cplusplus.com/reference/memory/unique_ptr/operator[]/
This member function is exclusive of the array-specialization of unique_ptr (unique_ptr<T[],D>). The non-specialized version does not include it.



> But I'm having issues
http://www.cplusplus.com/forum/articles/40071/#msg218019
Cheers.
Last edited on
Topic archived. No new replies allowed.