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:
#include <iostream>
#include <memory>
usingnamespace std;
unique_ptr<int[]> upArray;
int main()
{
int nArrayLength = 10;
upArray = newint[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 ^^
#include <iostream>
#include <memory>
usingnamespace std;
int main()
{
int nArrayLength = 10;
unique_ptr<int[]> upArray(newint[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.
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 = newint[nArrayLength](); // <- won't work... not explicit
upArray = unique_ptr<int[]>( newint[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 = newint[nArrayLength]();
.....................................
.......................................
};
I got know that it doesn't work. But is there a way to do those steps seperately?
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.