|
-
January 3rd, 2012, 12:55 PM
#1
std::vector initialization problem
Hello:
I have some code which needs to allocate a pointer to a list of arrays. The code which uses that pointer can throw exceptions. To prevent leaking the array, I would like to wrap the pointer with std::vector.
Code:
#include <vector>
void main(void)
{
int static const nUnits = 2;
// I want to use vector for it's RAII abilities.
std::vector<int[nUnits]> aUnits;
aUnits.resize(300);
int (* const pUnits)[nUnits] = &aUnits[0];
// Of course the below works fine, but then I have to wrap all the code
// which follows with an exception handler to prevent a memory leak.
// int (* const pUnits)[nUnits] = new int[300][nUnits];
// In the real program, the code which appears here and which uses
// pUnits has lots of places which might throw exceptions.
}
The problem is, I keep getting
Error 1 error C2440: '<function-style-cast>' : cannot convert from 'int' to 'int [2]' c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory 631
in STL code. That comes from a routine with the title
Code:
// TEMPLATE FUNCTION _Uninitialized_default_fill_n WITH ALLOCATOR
. I think the problem is that vector is trying to initialize the array with default values. In the real code, I don't need it initialized at all because I memcpy something else into it.
I looked around on the web to see if I could solve the problem by doing something with the second template argument to vector and found reference to a malloc_alloc object, but VS2010 doesn't seem to have that.
Nor do I see a way to allocate the array manually and then force vector to take ownership of it.
I'm about to give up and simply just wrap my routine with an exception handler so I can delete [] the array. Does anybody know how I can get vector or some other STL container to own the array?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|