简体   繁体   English

代码需要太多内存

[英]Code requires too much memory

I'm porting some code to another structure: 我正在将一些代码移植到另一个结构中:

class EnvironObject
{
   protected:
      vector<float> mX, mY, mXSpeed, mYSpeed;
      int mMaxObjects;

   public:
      virtual void init(int maxObjects);
      virtual void setLimit(int limit);
      virtual int getLimit();
      virtual void update(float arg) = 0;
};

void EnvironObject::setLimit(int limit)
{
   mMaxObjects = limit;

   mX.resize(limit, 0); mY.resize(limit, 0);
   mXSpeed.resize(limit, 0); mY.resize(limit, 0);
}

int EnvironObject::getLimit()
{
   return mMaxObjects;
}

void EnvironObject::init(int maxObjects)
{
    mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);

    fill(mX.begin(), mX.end(), 0);
    fill(mY.begin(), mY.end(), 0);
    fill(mXSpeed.begin(), mXSpeed.end(), 0);
    fill(mYSpeed.begin(), mYSpeed.end(), 0);

    /*mX.reserve(mMaxObjects * 1.5); mY.reserve(mMaxObjects * 1.5);
    mXSpeed.reserve(mMaxObjects * 1.5); mYSpeed.reserve(mMaxObjects * 1.5);*/

    mMaxObjects = maxObjects;
}

This is some basic class, now it's usage: 这是一些基本的类,现在它的用法是:

class Rain : public EnvironObject
{
    public:
        Rain(int maxDrops = 150);
        void update(float windPower);
};

Rain::Rain(int maxDrops)
{
    srand(time(NULL));

    IEnvironObject::init(maxDrops);
}

void Rain::update(float windPower)
{
    for (int i=0; i < mMaxObjects; i++)
    {
       mX[i] += mXSpeed[i];
       mY[i] += mYSpeed[i];

       mXSpeed[i] += windPower;
       mYSpeed[i] += G;

   // Drawing
    }
}

The objects Rain creates with default constructor (so, each array is 150 elements size) and then I'm calling setLimit(50) . Rain使用默认构造函数创建的对象(因此,每个数组的大小为150个元素),然后调用setLimit(50) The problem is that code fails almost each running with exception: 问题在于代码几乎每次运行都会失败,并带有异常:

terminate called after throwing an instance of 'std::bad_alloc'

And sometimes it segfaults at line: 有时它会在网上出现段错误:

mY[i] += mYSpeed[i];

I can't image what could it be, because the code is old and it worked. 我无法想象会是什么,因为代码很旧并且可以正常工作。 The new one is only base class. 新的只有基类。

And when I'm looking at RAM usage when starting app, I see almost +600 mb! 当我在启动应用程序时查看RAM使用情况时,我看到几乎+600 mb!

Look again at that function of yours: 再次查看您的功能:

void EnvironObject::init(int maxObjects)
{
    mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);
    //                                               ^
    // ...

    mMaxObjects = maxObjects;
}

You're using a not yet initialized variable. 您正在使用尚未初始化的变量。

A big problem with your class is that you are doing what's called two-phase construction. 课堂上的一个大问题是,您正在执行所谓的两阶段构造。 Your class EnvironObject has a compiler-supplied default constructor that creates an object with random values for all POD types ( mMaxObjects ). 您的类EnvironObject具有编译器提供的默认构造函数,该构造函数为所有POD类型( mMaxObjects )创建具有随机值的对象。 Users then need to call the init() method to really initialize the object. 然后,用户需要调用init()方法来真正初始化对象。 But that's what constructors are there for! 但这就是构造函数的作用!

void EnvironObject::EnvironObject(int maxObjects)
  : mMaxObjects(maxObjects)
  , mX(maxObjects), mY(maxObjects), mXSpeed(maxObjects), mYSpeed(maxObjects)
{
    /* these aren't necessary, std::vector automatically does this
    fill(mX.begin(), mX.end(), 0);
    fill(mY.begin(), mY.end(), 0);
    fill(mXSpeed.begin(), mXSpeed.end(), 0);
    fill(mYSpeed.begin(), mYSpeed.end(), 0);
    */
}

Derived classes can then use this constructor: 然后派生类可以使用此构造函数:

Rain::Rain(int maxDrops)
 : EnvironObject(maxDrops)
{
    srand(time(NULL));
}

Regarding this crash in the subscription mY[i] += mYSpeed[i] : 关于订阅mY[i] += mYSpeed[i]

This might happen when you are calling this function through a pointer that's pointing to nowhere. 当您通过指向无处的指针调用此函数时,可能会发生这种情况。

You're using mMaxObjects in init() before initializing it. init()之前,您正在init()使用mMaxObjects。 So it has a random value. 因此它具有随机值。

void EnvironObject::init(int maxObjects) 
{ 
   mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);  // you mean maxObjects here

I think you want to replace 我想你要更换

void EnvironObject::init(int maxObjects)
{
    mX = mY = mXSpeed = mYSpeed = std::vector<float>(mMaxObjects);

with

void EnvironObject::init(int maxObjects)
{
    mX = mY = mXSpeed = mYSpeed = std::vector<float>(maxObjects);

Notice the replacement of mMaxObject to maxObjects in the vector creation. 注意在向量创建中将mMaxObject替换为maxObjects。

一条评论虽然可能不会解决您的内存错误,但由于字段mX,mY,mXSpeed和mYSpeed似乎相关并且向量大小都相同,因此您应该考虑将它们合并为一个具有四个成员的结构,并具有包含多个这些结构实例的单个向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM