I have a program which creates a big amount of objects and inserts them in a vector. My idea was to create about 10.000 objects, but I found out that the program crashes after a few thousands. The amount of objects created before crashing is random an depends on if I modify any line in the code, so I suppose is a memory allocation problem related.
The object I am creating is this one:
class Object {
public:
//Needed by map
Object() {
}
Object(int newID, std::string newText) {
id = newID;
text = newText;
}
int getID() {
return id;
}
std::string getText() {
return text;
}
~Object() {
}
private:
int id;
std::string text;
};
Nothing special, as you can see. The program which creates the objects is as follows:
int main(int argc, char** argv) {
int numberOfElements;
long start;
long end;
long time1, time2, time3, time4, time5, time6;
numberOfElements = 7000; //7000<X<7050 Maximum reliable
{
//Measuring time for creation of 1000 elements in vector of objects,
cout << "VECTOR OF OBJECTS:" << endl;
start = getTimeInMicroseconds();
vector<Object> vectorOfObjects;
vectorOfObjects.reserve(10000);
for (int i = 0; i < numberOfElements; i++) {
cout << "Creating object " << i << endl;
Object object = *(new Object(i, "This is object "+i));
cout << "Created object " << i << endl;
vectorOfObjects.push_back(object);
cout << "Object inserted" << endl;
}
end = getTimeInMicroseconds();
time1 = end - start;
cout << "- Time to create " << numberOfElements << " objects = "
<< time1 << " microseconds" << endl;
}
return 0;
}
Again, something very simple. The amount of objects created before crashing depends on what I add after this code. Sometimes it crashes after 2000, sometimes, after 4000, sometimes after 7000... I suppose it is a memory allocation problem, but I don't know how to solve it.
I tried creating the objects as:
Object object(i, "text");
vectorOfObjects.push_back(object);
vectorOfObjects.push_back(Object(i, "text");
vectorOfObjects.push_back(*(new Object(i, "text")));
but none of them worked. Of course, I would prefer a method which creates dynamically those objects, like the two last examples I show here; I tried also with different containers, such as map or deque, but since the problem happens because of the creation of the object and not because of the container itself, it doesn't matter which container I use.
This is the core dump:
-bash-3.2$ pstack core
core 'core' of 5884: ./datastructuresperformance
d147646c strlen (8046eb8, 8055000, 8046ebc, d17c34ed) + c
08051fdf main (1, 8047264, 804726c, 8051ddf) + f7
08051e27 _start (1, 80473b8, 0, 80473d4, 80473ef, 8047441) + 67
-bash-3.2$ pmap core
core 'core' of 5884: ./datastructuresperformance
08044000 16K rwx-- [ stack ]
08050000 20K r-x-- /export/home/dcs/SolStudioProjects/DataStructuresPerformance/dist/Release/OracleSolarisStudio-Solaris-x86/datastructuresperformance
08064000 8K rwx-- /export/home/dcs/SolStudioProjects/DataStructuresPerformance/dist/Release/OracleSolarisStudio-Solaris-x86/datastructuresperformance
08066000 280K rwx-- [ heap ]
D1450000 1088K r-x-- /lib/libc.so.1
D1560000 32K rwx-- /lib/libc.so.1
D1568000 8K rwx-- /lib/libc.so.1
D1570000 292K r-x-- /lib/libm.so.2
D15C8000 16K rwx-- /lib/libm.so.2
D15D0000 48K r-x-- /usr/lib/libCrun.so.1
D15EB000 8K rwx-- /usr/lib/libCrun.so.1
D15ED000 20K rwx-- /usr/lib/libCrun.so.1
D1600000 24K rwx--
D1610000 1244K r-x-- /usr/lib/libCstd.so.1
D1750000 4K rwx--
D1756000 216K rwx-- /usr/lib/libCstd.so.1
D1790000 4K rwx--
D17A0000 4K rw---
D17B0000 4K rw---
D17BF000 176K r-x-- /lib/ld.so.1
D17F0000 4K rwx--
D17FB000 8K rwx-- /lib/ld.so.1
D17FD000 8K rwx-- /lib/ld.so.1
total 3532K
It should not be a problem related to the amount of memory, since the maximum amount of memory used by this program so far is much lower than the 1GB of this machine.