Out of Memory/Crash issue

Which of below code , assuming there is no out of memory can happen, say if it is correct, if it will crash or performs more than one memory allocation?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

//Code 1
vector<int> v;
v.reserve(2000);
for(int i =0; i<2000; i++)
{
  v.push_back(i);
}


//Code 2
vector<int> v;
v.reserve(2000);
for(int i =0; i<2000; i++)
{
  v[i] = i;
}

//Code 3
vector<int> v;
v.resize(2000);
for(int i =0; i<2000; i++)
{
  v.push_back(i);
}


//Code4
vector<int> v;
v.resize(2000);
for(int i =0; i<2000; i++)
{
  v[i] = i;
}

and your thoughts on this are...
My understanding is that here reserve() to pre allocate a suitably large memory block to accomodate a given number of the objects. So vector here will always preallocate a memory block using reserve(). In this case, vector holds a value between 0 to 2000. So I think below Code1 wont have any issues as here there are 0 allocations during for loop, hence its correct. I suspect either Code2, Code3 or Code4 might have issues.
Please advise.

1
2
3
4
5
6
7
8
//Code 1
vector<int> v;
v.reserve(2000);
for(int i =0; i<2000; i++)
{
  v.push_back(i);
}
Last edited on
The point here is the difference between reserve(...) and resize(...) which you may research.

[EDIT]
What does push_back(...) do and how could this be an issue?
What is Code5?
Last edited on
There is no Code 5, rather Code2,3,4. I dont have much idea on vector as I am new to this.
The difference between reserve(...) and resize(...) is that reserve(...) does not modify the size while resize(...) of course does.

To grasp what happens within each code you may add to each code:

std::cout << "size: " << v.size(); << '\n';

That may make things clearer
also what does [] do? this is just as important because 4) ... needs attention to details.

I dont have much idea on vector as I am new to this.

that is exactly why you need to do this yourself. It is important, vector is one of the most used things in the c++ language. You can study the things you need here: https://www.cplusplus.com/reference/vector/vector/
Last edited on
So I think below Code1 wont have any issues


OK.

I suspect either Code2, Code3 or Code4 might have issues.


Well yes, some of them do have issues - but which one(s)? Don't suspect - investigate and understand and then make a statement as to yes or no because...
> if it will crash
In c++ you may never have that guarantee, a lot of runtime errors end in the realm of undefined behaviour instead
if you do get a crash, consider yourself lucky to have an opportunity for debug
I am not able to figure out which among these code performs more than one memory allocation? I dont see any crash while running these codes separately. In Code2 output is 0,
does that mean it went out of memory?

Below is my Code1 output

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



int main()
{
std::vector<int> v;
v.reserve(2000);
for(int i =0; i<2000; i++)
{
  v.push_back(i);
}
    std::cout << "size: " << v.size() << '\n';

    return 0;
}


OUTPUT : 2000



Below is my Code2 output

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



int main()
{
std::vector<int> v;
v.reserve(2000);
for(int i =0; i<2000; i++)
{
  v[i] = i;
}
    std::cout << "size: " << v.size() << '\n';

    return 0;
}


OUTPUT :0



Below is my Code3 output

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



int main()
{
std::vector<int> v;
v.resize(2000);
for(int i =0; i<2000; i++)
{
  v.push_back(i);
}
    std::cout << "size: " << v.size() << '\n';

    return 0;
}

OUTPUT : 4000



Below is my Code4 output

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



int main()
{
std::vector<int> v;
v.resize(2000);
for(int i =0; i<2000; i++)
{
  v[i] = i;
}
    std::cout << "size: " << v.size() << '\n';

    return 0;
}


OUTPUT : 2000
Last edited on
if the size is 0, then the vector is empty

¿what operations may perform an allocation?
which among these code performs more than one memory allocation?
Well, in one of your codes you input 2000 and the output is 4000...

I dont see any crash while running these codes separately.
Unfortunately you cannot rely on the certain outcome when a crash occurs.

In Code2 output is 0,
does that mean it went out of memory?
Unexpected output comes next to a crash, doesn't it?
Can you tell why it is 0? And no, it didn't run out of memory.

Where input is 2000 and output is 2000 are the best results. What you need to explain are the other results.
OP

code1 - memory is allocated (but not used) for 2000 items. So before the push_back size is 0 and 2000 elements can be pushed without memory-reallocation. After size is 2000 and all memory is used.

code2 - again memory is allocated (but not used) for 2000 items. So before assignments, size is 0. However the assign assumes that memory is used when it is not. Hence at the end size is still 0 because as far as vector is concerned, nothing has been used. This is also 'bad' code accessing vector memory that hasn't been 'used'.

code3 - memory is allocated for 2000 items and it is marked as 'used'. The size is hence 2000. Push-back starts at the end of 'used' so starts after the existing 2000 elements. Thus 2000 elements are added to the vector after the existing 2000 giving a final of 4000. The 2000 elements added will cause some memory re-allocation.

code4 - memory is allocated for 2000 items and marked as 'used'. The size is hence 2000. The assignments use this 'used' memory so OK. The final size is 2000.

Note the issue in code 2 as opposed to OK code 4
The difference between reserving the space and resizing is that with resizing, you initialize each element twice; once on growing the vector, and again on assignment.
Thanks seeplus to clarify in details. I got a better understanding how it internally operates now.
Thanks everyone for your suggestions. I will close this thread as solved now.
Topic archived. No new replies allowed.