【问题标题】:Is a pointer to a stack object deleted when out of scope?超出范围时是否会删除指向堆栈对象的指针?
【发布时间】:2018-05-02 10:30:23
【问题描述】:

我很难准确地理解我应该对我的类中的指针成员做什么。我知道用 new[] 创建的任何指针都必须用 delete[] 删除

但是,如果我的指针指向在堆栈上创建的对象的地址怎么办?我必须删除它吗?或者当类被销毁时它会被删除。如果是这样,我应该以什么方式删除它?澄清问题,这是我的一些代码。

移动头文件:Moves.h

#pragma once

#include "ShuffleBag.h"

class Character;

class Moves
{
private:
    Character* pm_User;
    ShuffleBag m_HitChances;

public:
    Moves (Character& user);
    ~Moves ();
};

我们可以看到我有一个指向字符对象的指针成员。

Moves 源文件:Moves.cpp

#include "Moves.h"
#include "Character.h"



Moves::Moves (Character& user)
{
    m_HitChances = ShuffleBag ();
    m_HitChances.Add (true, 8);
    m_HitChances.Add (false, 2);

    pm_User = &user;
}

Moves::~Moves ()
{
}

在这里我们可以看到我将这个指针分配给了字符对象的传入引用的地址。

字符头文件:Character.h

#pragma once

#include "Moves.h"
#include "ShuffleBag.h"

class Character
{
public:
    int m_Health;
    int m_Energy;
    Moves* pm_Moves;

public:
    Character ();
    Character (int health, int energy);
    ~Character ();

};

同样,这里我有一个指向该角色移动集的指针。这是因为移动没有默认构造函数。

字符源文件:Character.cpp

#include "Character.h"



Character::Character ()
{
    m_Health = 100;
    m_Energy = 50;

    pm_Moves = &Moves (*this);
}

Character::Character (int health, int energy)
{
    m_Health = health;
    m_Energy = energy;

    pm_Moves = &Moves (*this);
}

Character::~Character ()
{
}

在这里,我为这个指针分配了新创建的 Moves 对象的地址。所以我的 TL;DR 格式的问题是这样的:

我的指针是否指向堆栈对象,当类死亡时,指针本身会吗?还是我必须删除它们?

【问题讨论】:

  • 首先,课程永不消亡。实例(=对象)有时会这样做。其次,您必须确定实例 Character 是否是 Moves 对象的 所有者。如果是这样,如果应该在析构函数中显式删除它(在释放字符时调用)。第三,你最好使用智能指针。
  • 只有操作员new创建的对象才需要是deleted。需要注意的一件事是指向超出范围的对象的指针 - 该指针无法取消引用,因为它指向不再存在的对象。

标签: c++ pointers


【解决方案1】:

需要在new 返回的指针上调用delete。这条规则也不例外。

在你的情况下,

pm_Moves = &Moves(*this);

正在分配一个指向 匿名临时 Moves(*this); 的指针。就是那个在声明之后立即失效的指针!将该指针用于任何事物的程序行为是undefined

所以你显然需要重新设计这一切。重构时考虑查看std::unique_ptr

【讨论】:

  • 我明白了,我明白了。您的答案与其他 cmets 相结合是有道理的。我肯定会重新设计它,并计划使用智能指针。出现指针的唯一原因是因为我试图让角色成为 Moves 类中的引用成员。但是,当我尝试使用 *this 传递字符引用时,它抱怨删除了一个函数。似乎需要更多地了解这些,感谢您的解释。
【解决方案2】:

我知道用 new[] 创建的任何指针都必须用 删除[]

我一遍又一遍地看到这种困惑。 你不删除指针你删除对象。或者更好地理解,如果您谈论malloc / free:您不释放指针,而是释放内存。指针只是指向您需要删除的对象/内存。

例如:

int a = 24;          // just an int

int* p1 = new int;   // new allocates memory for an int,
                     // creates an int at that location
                     // and then returns a pointer to this newly created int
                     // p1 now points to the newly created int

int* p2 = p1;        // p2 now also points to the created int
p1 = a;              // p1 now points to a

// what do we delete now? p1?
// no, we don't delete pointers,
// we delete objects dynamically created by new
// what pointer points to those objects?
// at this line it is p2
// so correct is:

delete p2;

【讨论】:

    【解决方案3】:

    这个类中没有创建新的动态元素,所以你不能销毁它。

    【讨论】:

    猜你喜欢
    • 2014-12-02
    • 2014-10-16
    • 1970-01-01
    • 2013-01-29
    • 2016-02-10
    • 2016-11-14
    • 1970-01-01
    • 2013-02-11
    • 2012-10-07
    相关资源
    最近更新 更多