1

Suppose the following data structure typedef std::vector<std::vector<int> > MYARRAY is defined. Then for the variable MYARRAY var, how can I allocate memory for this variable before pushing data in it. For example,

std::vector<int> p1; 
p1.push_back(1);
p1.push_back(2);
std::vector<int> p2;
p2.push_back(22);
p2.push_back(33);
var.push_back(p1);
var.push_back(p2);

If we do not allocate memory for var, then it will allocate memory automatically. So how can I allocate memory for this var before pushing data insider it? If it is std::vector<int> var2, I can just use var2.reserve(n) to allocate memory before using it.

EDIT: Two suggestions have been made, but neither can work:

  • Solution 1: allocate memory for each element

var.reserve(3); for(int i=0; i<3; i++) var[i].reserve[20];

I use VC 2010 to compile and run the codes in the debug mode, and the following error message is given:enter image description here

  • Solution 2: create the object in the beginning

       std::vector<std::vector<int> > var(3, std::vector<int>(5));
    

    After you created this variable, you can see this variable in VC 2010: enter image description here Its contents are already there. Therefore, if you push data on this variable, it will allocate memory once again.

EDIT 2:

Someone is interested in why I need allocate memory before using this variable, and the main reason is because of run-time library of windows. The variable var is defined in a executable program as an empty variable, and its contents are given by a function defined in a dynamic library. If both are using dynamic run-time library, it will not be an issue. But in my case both are linked with static run-time library, which means that each module is in charge of its memory allocation. Since var is defined in the executable program, it also should take care of its memory allocation.

feelfree
  • 9,501
  • 18
  • 76
  • 143
  • 2
    So what's wrong with `var.reserve(n)`...? – scohe001 Aug 28 '14 at 15:43
  • 1
    Why do you need to allocate memory for something that is handled automatically? – edtheprogrammerguy Aug 28 '14 at 15:43
  • Are you trying to "allocate memory" for each vector element inside MYARRAY? – jliv902 Aug 28 '14 at 15:46
  • @Josh var.rserve(n) will allocate memory for var[0]...var[n], but not for each element for example var[0] itself, it has not allocated memory. – feelfree Aug 28 '14 at 15:46
  • If this collection is fixed (i.e. cannot be expanded once created) then `std::array` might be a better choice for you. – trojanfoe Aug 28 '14 at 15:53
  • @feelfree - Maybe you can tell us *why* you need to do this and not just use `std::vector` as it is? – PaulMcKenzie Aug 28 '14 at 16:34
  • @PaulMcKenzie Thanks for the comments. I edited the question to make it clear why I need to allocate memory for this structure. – feelfree Aug 28 '14 at 16:41
  • @feelfree - I don't see a "why". All I see is what you want to do without telling us why you want to do it. What is the higher-level goal here? The usage of `vector::reserve` should be self-explanatory, or at least, explained rather well in most online references to `vector`. – PaulMcKenzie Aug 28 '14 at 16:43
  • @PaulMcKenzie, thanks for your comments, and I edited the question. – feelfree Aug 28 '14 at 16:47
  • 1
    @feelfree Your error in your post is because you've mixed up `reserve` with `resize`. `reserve` does *not* give you access to the space you've allocated. You still have to call `push_back`, `insert`, `resize` etc. to fill in the space given to you. What `reserve` does is to make operations such as `push_back` `resize` and `insert` not need to allocate memory themselves. – PaulMcKenzie Aug 28 '14 at 16:51

3 Answers3

3

The way you reserve memory for a vector is independent of what type the vector contains. If you want to reserve space for n elements in MYARRAY, call MYARRAY.reserve(n);.

Its contents are already there. Therefore, if you push data on this variable, it will allocate memory once again.

Right. Do you want to reserve memory or not? If you want to reserve memory, you'll have to use the memory you reserved. In order for you to "push data on this variable", you'd have to have the data somewhere, somewhere other than the memory you reserved. If you reserve memory and use that memory, you'll never have anything to push, because that would imply that you have something someplace other than in the memory you reserved that you need to add to the vector, which you couldn't possibly have.

You basically have three choices:

1) Don't reserve memory. Assemble the objects wherever you want and then push them into the vector. vec.push_back(myVector);

2) Reserve memory. Assemble the objects in place in the vector. vec[n].push_back(myInt);

3) Reserve memory. Assemble the objects wherever you want and then assign them into the memory you reserved. vec[n]=myIntVector

Notice that in none of these cases do you reserve memory and then push into the vector.

David Schwartz
  • 166,415
  • 16
  • 184
  • 259
  • var.rserve(n) will allocate memory for var[0]...var[n], but not for each element for example var[0] itself, it has not allocated memory – feelfree Aug 28 '14 at 15:50
  • That's correct. If you want to reserve memory for `var[0]`, then call `var[0].reserve`. – David Schwartz Aug 28 '14 at 15:51
  • @ David Schwartz I tried with VC 2010, it will intriguer Debug Assertion Failed message. – feelfree Aug 28 '14 at 15:54
  • @feelfree Show the code that failed. We can't debug code we can't see. (Did you try to reserve space inside an object before creating that object? That can't work.) – David Schwartz Aug 28 '14 at 16:00
  • @feelfree See my updates. Your objection doesn't make sense. If you reserved memory, your object would be in the memory you reserved. You wouldn't have some object somewhere else that you need to add to the vector. So you wouldn't be trying to push anything. So your objection that you can't push is invalid. – David Schwartz Aug 28 '14 at 16:26
2

Like you already pointed out, std::vector has the reserve method that will reserve space for more data items. If you were to do p1.reserve(3) the vector would attempt to allocate space for 3 integers. If you run var.reserve(3), var will attempt to allocate space for 3 std::vector<int>'s, which is what it sounds like you want to do.

To allocate for the std::vector's inside of var, you could do:

for(int x=0; x<var.size(); x++) var[x].reserve(n);

EDIT

If you want to allocate space before inserting the vectors, you can declare var as:

std::vector<std::vector<int> > var(VEC_COUNT, std::vector<int>(n));

And then copy the new vectors in.

Community
  • 1
  • 1
scohe001
  • 13,879
  • 2
  • 28
  • 47
  • thanks, but it will not work as it is illegal to call var[x].reserve(n) as we have not created the element var[x] yet although its memory has been allocated – feelfree Aug 28 '14 at 15:49
  • thanks, but i do not think it is right. std::vector > var(VEC_COUNT, std::vector(n)); will create the object rather than allocate memory for it. – feelfree Aug 28 '14 at 15:59
  • @feelfree It will create a `vector` with space for `VEC_COUNT` vectors, each with capacity `n`. – scohe001 Aug 28 '14 at 16:00
  • @Josh ... each *of size `n`*. The single-parameter constructor sets the vector's size, not (just) capacity. – Angew is no longer proud of SO Aug 28 '14 at 16:10
  • @Angew it doesn't matter what the `size` is here. When he uses the `=` operator to copy in the new vectors, the size will be corrected. – scohe001 Aug 28 '14 at 16:12
  • @feelfree, for the first suggestion, the data must already be in `var`. And for the second, you're going to need to copy the new vectors in with the assignment operator. – scohe001 Aug 28 '14 at 16:22
2

You cannot reserve space for vector's data before the vector exists. I believe you're looking for this:

void reserve(MYARRAY &arr, size_t dim1, size_t dim2)
{
  arr.resize(dim1);
  for (size_t idx = 0; idx < dim1; ++idx) {
    arr[idx].reserve(dim2);
  }
}
Angew is no longer proud of SO
  • 156,801
  • 13
  • 318
  • 412
  • I agree with you and therefore I do not think it is possible to allocate memory for this data structure. – feelfree Aug 28 '14 at 16:17