Arrays and smart pointers

I'm writing quite a large program that needs to work with very large arrays (up to 100 million integers). It is necessary that i can access every point of the array directly (without running through the array) and that I use as little memory as possible. I first wrote the program with pointers that point to allocated heap memory. It works fine but now I wanted to use smart pointers instead (so I'm sure to have no memory leaks). Sadly I had to learn, that some thing that worked with pointer dont work with unique pointers. I don't get what I'm doing wrong. Here's a simple visualization of my problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <memory>

using namespace std;

unique_ptr<int[]> upArray;

int main()
{
	int nArrayLength = 10;

	upArray = new int[nArrayLength](); //Allocate  an Array with 10 int values initialized with 0;

	for (int i = 0; i < nArrayLength; i++) // fill the array with 0,1,2,3,4,5,6,7,8,9 and display the numbers
	{
		*(upArray + i) = i;
		cout << *(upArray + i);
	}
	return 0;
}


There are 2 things that do not work how I would like the to:
1. It wants me to assign the heap memory in one step: unique_ptr<int[]> upArray(new int[nArrayLength]); But I'd like to have the unique_ptr in my Class_Declaration before I know the array length and allocate the memory later.
2. *(upArray + i) = i;
cout << *(upArray + i);
Those lines don't work! How else can I do it.

PS: That's my first post here :) I hope the topic didn't exist already (didn't find anything). Please excuse my english and my weak programming slang ^^
unique_ptr's ctor is explicit. You just have to explicitly construct it:

 
upArray = unique_ptr<int[]>( new int[nArrayLength]() );



Also, ew @ hungarian notation.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <memory>

using namespace std;



int main()
{
	int nArrayLength = 10;

	unique_ptr<int[]> upArray(new int[nArrayLength]()); //Allocate 10 an Array with 10 int values initialized with 0;

	for (int i = 0; i < nArrayLength; i++) // fill the array with 0,1,2,3,4,5,6,7,8,9 and display the numbers
	{
		*(upArray + i) = i;
		cout << *(upArray + i);
	}
	return 0;
}



The rest of the program still doesn't work. Also I would like to have the pointer as a protected element of a class. The memory should be initialized and overwritten in a member function. The size of the memory isn't known until the memberfunction is entered.
The rest of the program still doesn't work.


Whoops. I missed that part. If you treat it like an array instead of doing weird pointer math it'll work fine:

1
2
3
4
5
6
7
// get rid of this crap:
//*(upArray + i) = i;
//cout << *(upArray + i);

// use it like an array:
upArray[i] = i;
cout << upArray[i];



Also I would like to have the pointer as a protected element of a class.


Nothing is stopping you from doing that. The only difference is that when you assign it... you have to assign it through a constructor because it has to be explicit:

1
2
3
upArray = new int[nArrayLength]();  // <- won't work... not explicit

upArray = unique_ptr<int[]>( new int[nArrayLength]() );  // <- will work.  Is explicit 
Thx. Using it like an array worked. But still... I'll be more explicit:
I have a class called State
In the header state.h i define:
1
2
3
4
5
6
7
8
9
Class State
{
public:
......................
......................
void getBlocks(int nArrayLength); // this function will fill the array with values
protected:
unique-ptr<int[]> upArray //I don't know what arraylength yet
}

in state.cpp I implement the function:
1
2
3
4
5
6
7
State::getBlocks(int nArrayLength)
{
//now i know the needed length so I want to allocate the memory now
upArray = new int[nArrayLength]();
.....................................
.......................................
};


I got know that it doesn't work. But is there a way to do those steps seperately?
I got know that it doesn't work. But is there a way to do those steps seperately?


Yes... I showed how to do it twice already =P:

1
2
3
upArray = new int[nArrayLength]();  // <- won't work... not explicit

upArray = unique_ptr<int[]>( new int[nArrayLength]() );  // <- will work.  Is explicit  
Last edited on
As per your requirements you really ought to consider using a std::deque. Its space requirements really are fairly small, and it utilizes memory very well.
Topic archived. No new replies allowed.