创建2000个对象后C ++程序崩溃 [英] C++ program crashes after creating 2000 objects

查看:64
本文介绍了创建2000个对象后C ++程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,该程序创建大量对象并将其插入向量中.我的想法是创建约10.000个对象,但是我发现该程序在数千个之后崩溃.崩溃前创建的对象数量是随机的,取决于我是否修改代码中的任何行,因此我认为这与内存分配问题有关.

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;
}

再次,很简单.在崩溃之前创建的对象数量取决于我在此代码之后添加的内容.有时在2000年之后崩溃,有时在4000之后,有时在7000之后崩溃...我想这是一个内存分配问题,但是我不知道如何解决.

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.

我尝试将对象创建为:

Object object(i, "text");
vectorOfObjects.push_back(object);

vectorOfObjects.push_back(Object(i, "text");
vectorOfObjects.push_back(*(new Object(i, "text")));  

但是他们都不起作用.当然,我更喜欢一种动态创建这些对象的方法,就像我在此展示的最后两个示例一样;我也尝试过使用其他容器,例如map或deque,但是由于问题的发生是由于对象的创建而不是容器本身,所以我使用哪个容器都没有关系.

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.

这是核心转储:

-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

这与内存量无关,因为到目前为止该程序使用的最大内存量远低于本机的1GB.

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.

推荐答案

  1. *new T()被称为内存泄漏运算符.您不想动态分配对象,而只想构造一个对象(该对象将被向量破坏)
  2. 您不能使用"string" + i,因为它将对char(&)[7](即"string")进行指针算术运算.从逻辑上讲,"string" + i表示&("string"[i]),当i大于字符串文字长度时,会导致未定义的行为(在您的情况下,崩溃).
  1. *new T() has been dubbed the memory leak operator. You don't want to dynamically allocate an Object, instead, you just want to construct one (that will be destructed with the vector)
  2. You can't use "string" + i because it will do pointer arithmetic on the char(&)[7] (that is "string"). Logically, "string" + i says &("string"[i]), which causes undefined behaviour (in your case, crash) when i is > than the length of the string literal.

除非您知道自己在做什么,否则请不要在C ++中使用new:

Don't use new in C++ unless you know what you're doing:

Object object = Object(i, "This is object " + to_string(i));

更好的是,如果您的编译器不是很旧,请考虑使用emplace_back:

Better yet, consider using emplace_back if your compiler isn't very old:

    vector<Object> vectorOfObjects;
    vectorOfObjects.reserve(100000000ul);
    for(int i = 0; i < numberOfElements; i++) {
        vectorOfObjects.emplace_back(i, "This is object " + to_string(i));
    }

这篇关于创建2000个对象后C ++程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆