问题
I'm migrating some code from c to c++.
I've changed some malloc
calls for structure memory allocation to new
calls.
Basically before the code I'm porting was malloc'ing arrays that contain multiple sets of frame coords, each a couple hundred thousand floats in length -- so the total array length could be in the tens of millions of coordinates.
What kind of structure/container should I use?
And what kind of protections do I need to catch memory-related errors?
Edit 1
I've retitled/rephrased the question to more accurately reflect what I'm trying to do.
I'm think some sort of 2D list-like structure might do the trick... possibly a std::deque of std::deque(s)?
回答1:
Answer is std::vector
.
You don't need that much memory actually (or you have some memory constrained platform, I assume you would have told us in that case). Vector is perfectly fine for this purpose. And you don't have to manage the memory yourself.
You can use vectors of vectors if you want to manage several of them at once.
But some 10^6s floats is definitely not a big deal nowadays.
Update: One more thing if you go with deque
. Please don't access deque
objects by index in loops. Actually deque
is strong at inserting at both sides, but not at accessing objects by index. And probably not at inserting objects in the middle, as I have seen somehere.
回答2:
When allocating through new
fails it throws std::bad_alloc
. But do you really requite so many floats
in continuos memory location. If not you can take a look at other datastructures such as std::deque
or std::list
EDIT: list
doesn't make sense as you are asking for a replacement for an array.
回答3:
EDIT: If you want a C++ style matrix then I would first recommend boost::matrix
:
boost::matrix<float> my_matrix(n, m);
If you can't use boost, then I would recommend a vector of vectors.
std::vector<std::vector<float> > m_matrix(n, std::vector<float>(m));
(notice the space after the first >, this is necessary because >> is an operator in C++).
You can also use a deque of deques (or a combination of vectors and deques). The big difference is that vectors guarantee that the elements are stored in a contiguous block of memory where a deque does not. This may or may not be a good thing for your purposes.
Deques are also more efficient at insert new elements in the middle of the structure.
Yes, a call to new can fail. Generally if a call to new fails it throws a std::bad_alloc exception, which you can catch. Since you are migrating code from c to c++, it might be easier to use std::nothrow, which will cause new to return a null pointer (much like malloc).
try
{
my_array = new float[num_points];
}
catch(std::bad_alloc &exp)
{
...
}
or
my_array = new (std::nothrow) float[num_points];
if(m_array == NULL)
{
...
}
来源:https://stackoverflow.com/questions/3737138/how-best-to-store-very-large-2d-list-of-floats-in-c-error-handling