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

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

问题描述

我希望你可以帮助我解决这个问题。

I hope you can help me with this problem.

我有一个程序创建了大量的对象,并将它们插入向量。我的想法是创建约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.

创建的是这个:

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...

如果您需要任何其他信息,请让我知道。谢谢。

If you need any other information, please let me know. Thank you.

推荐答案


  1. * new T()已被称为内存泄漏运算符。您不想动态分配对象,而只是想要构造(将被向量破坏)

  2. 您不能使用string+ i ,因为它会对 char(&)[7] string)。逻辑上,string+ i 表示&(string[i]) c> >

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

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

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