小编Mol*_*lma的帖子

什么是Windows等效的CppCheck?

我有一个极端的问题.

我已经在游戏上工作了大约两年(20000多行代码),最近我注意到了大量的内存泄漏.问题是我无法追踪他们中的每一个,因为我的游戏太大了......

我已经四处搜索并注意到CppCheck在我的情况下会很有用,但问题是因为我使用的是Windows,所以我不能使用CppCheck(仅适用于linux).

我想知道是否有一个库或插件是CppCheck等效的Windows,或者可能是一种在Windows上使用CppCheck的方法.

我提出的所有可能性,以及其他问题的解决方案(例如使用std :: deque的智能指针等)都意味着我的程序很小或更合适:重写我的整个程序,就像我一样 - 真的 - 不想做...

IDE:代码块10.05

编译:MinGW 3.81 GCC 4.4.1

c++ memory-leaks cppcheck

6
推荐指数
1
解决办法
1338
查看次数

"预定义"和使用命名空间和std :: shared_ptr的正确方法是什么?

我一直很难找到类似这个问题的东西,所以我会问这里.

我有一个包含十几个源/头文件的项目.我遇到的主要问题是预定义我在命名空间中创建的类.代码如下:

"GlobalIncludes.h"

/*include dependencies and library headers...*/

/*[Note 1]How would I predefine the classes inside namespaces?*/

typedef std::tr1::shared_ptr<Class1> ClassPtr1;//[Note 2]
typedef std::tr1::shared_ptr<Class2> ClassPtr2;//[Note 2]

/*[Note 2]What is the correct way to predefine the shared_ptr's?*/

#include "Class1.h"
#include "Class2.h"
Run Code Online (Sandbox Code Playgroud)

"Class1.h"

namespace myNamespace
{
    class Class1
    {
        /*variables and functions*/
        void doSomething(...);
        Class2 exampleObject;
    };
}
Run Code Online (Sandbox Code Playgroud)

"Class2.h"

namespace myNamespace
{
    class Class2
    {
        /*variables and functions*/
    };
}
Run Code Online (Sandbox Code Playgroud)

如果这听起来有点令人困惑,我提前道歉......基本上我想知道是否有可能预定义namespace myNamespace同时声明shared_ptr的类.如果可以,我该如何做,并在源中正确使用它们?

c++ namespaces class shared-ptr

1
推荐指数
1
解决办法
357
查看次数

是否可以从带有C++ 11 for for循环的向量中擦除?

好的.为了其他(更简单但不够解释)问题,这可能看起来像,我不是在问这是可能的还是不可能的(因为我已经发现了),我在问我是否有更轻松的替代品题.

我所拥有的是主要类,在主类中,有一个引用"世界地图"类的变量.实质上,这个"WorldMap"类是其他类变量的容器.主类完成所有循环并更新所有相应的活动对象.在这个循环中,我需要删除一个向量的对象,该对象位于递归容器集的深处(如提供的代码所示).重复必须将必要的变量作为指向另一个指针(等等)的指针引用我需要的特定对象,然后将其删除(这是我在切换到C++之前使用的概念)将是非常繁琐的. 11)所以我有一个循环范围(也在代码中显示).我的示例代码显示了我已经到位的想法,我希望减少单调乏味以及使代码更具可读性.

这是示例代码:

struct item{
    int stat;
};

struct character{
    int otherStat;
    std::vector<item> myItems;
};

struct charContainer{
    std::map<int, character> myChars;
};

int main(){
    //...
    charContainer box;
    //I want to do something closer to this
    for(item targItem: box.myChars[iter].myItems){
        //Then I only have to use targItem as the reference
        if(targItem.isFinished)
            box.myChars[iter].myItems.erase(targItem);
    }
    //Instead of doing this
    for(int a=0;a<box.myChars[iter].myItems.size();a++){
        //Then I have to repeatedly use box.myChars[iter].myItems[a]
        if(box.myChars[iter].myItems[a].isFinished)
            box.myChars[iter].myItems.erase(box.myChars[iter].myItems[a]);
    }
}
Run Code Online (Sandbox Code Playgroud)

TLDR:我想通过使用C++ 11中显示的新的循环范围来消除重复调用完整引用的繁琐.

编辑:我不是想一次删除所有元素.我问我如何在第一个循环中删除它们.当我在外部完成它们时(通过if语句),我将删除它们.我如何删除特定元素,而不是所有元素?

c++ for-loop vector erase c++11

1
推荐指数
1
解决办法
257
查看次数

标签 统计

c++ ×3

c++11 ×1

class ×1

cppcheck ×1

erase ×1

for-loop ×1

memory-leaks ×1

namespaces ×1

shared-ptr ×1

vector ×1