#include <iostream>
#include <cassert>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <Windows.h>
using namespace std;
char randomLetter()
{
srand(time(0));
char rValue;
while(1)
if((rValue=(rand()/129)) > 31)
return rValue;
}
int main()
{
vector<char> meegaString;
for(int i=0; i < 10000000000; i++)
{
meegaString.push_back(randomLetter());
if(!(i%10000000))
cout<<"There are: " <<i+1<<" chars in the list"<<endl;
}
system("pause");
return 0;
}
RAM usage before running this program was approximately 2500/8000 MB.
When it comes to 3200, the following exception is thrown:
Unhandled exception at 0x773c15de in Resources gormandizer.exe:
Microsoft C++ exception: std::bad_alloc at memory location
0x0045f864..
1) Why this program didn’t fill the whole available memory , although it was working on a 64 bit OS?
2) Why only 26 percent of processor(intel core i5) were in usage?
As noted, the elements of a vector are stored contiguously. Also, depending on the memory allocation algorithm used in your implementation of
std::vector, it is probably attempting to pre-allocate memory ahead of time; allocating more memory than is being used to cut down on the number ofmalloc/newcalls. As such, it might be getting to the point where it’s requesting more memory than a 32-bit OS can support (which would explain why a 64-bit process would work, but a 32-bit process wouldn’t, despite having enough memory available).Your process is running on one core out of 4, and it’s quite busy, hence it’s taking up approximately 25% of the CPU time. Other processes would make up the rest.
See also: Being Smart About Vector Memory Allocation