说我做这样的事情
for(int i = 0; i < 10; i++)
{
//create a pointer object using new
//use the object
}
Run Code Online (Sandbox Code Playgroud)
在循环中使用它之后是否需要删除指针对象?我当时想,如果我没有删除它,它将继续创建一个新对象10次,然后对象就会挂在那里,吃掉资源.
编辑:有人可以解释为什么添加删除操作符不会更改Valgrind输出?请不要让我阅读C++书籍,我浏览了一对夫妇并且没有在那里找到答案.
我是C++的新手,来自java世界,我似乎无法理解如何使用new和delete运算符.
我有一个方法(myMethod())启动一个类,MyClass.在MyClass中,使用new和new []运算符创建了一些数组和很少的其他对象.最初我没有为这个类编写析构函数(我假设当控件从我的方法返回到main时,这些MyClass对象以及与它相关的所有内容都会自动"释放").
当我用Valgrind运行我的程序时,这是我得到的泄漏摘要:
==9446== LEAK SUMMARY:
==9446== definitely lost: 1,957,019 bytes in 38 blocks.
==9446== indirectly lost: 4,171,184 bytes in 3,040 blocks.
==9446== possibly lost: 0 bytes in 0 blocks.
==9446== still reachable: 91,984 bytes in 991 blocks.
==9446== suppressed: 0 bytes in 0 blocks.
==9446== Reachable blocks (those to which a pointer was found) are not shown.
==9446== To see them, rerun with: --show-reachable=yes
Run Code Online (Sandbox Code Playgroud)
所以我添加了一个析构函数,它删除所有数组并将对象设置为null.我没有在MyClass对象上显式调用delete.现在,当我使用Valgrind运行我的程序时,这是我得到的泄漏摘要.
LEAK SUMMARY:
==9223== definitely lost: 1,957,019 bytes in 38 blocks.
==9223== indirectly …Run Code Online (Sandbox Code Playgroud) 我编译并运行下面粘贴的代码,令人惊讶的是它没有错误.(g ++/linux)删除的对象如何让一些成员仍然可用?这是正常的行为吗?
#include <iostream>
using namespace std;
class chair {
public:
int height;
int x;
int y;
chair() {
before = last;
if(last!=NULL)
last->after = this;
else
first = this;
last = this;
after = NULL;
}
~chair() {
if(before != NULL)
before->after = after;
else
first = after;
if(after != NULL)
after->before = before;
else
last = before;
}
chair* before;
chair* after;
static chair* first;
static chair* last;
};
chair* chair::first;
chair* chair::last;
int main() {
chair *room …Run Code Online (Sandbox Code Playgroud) class Foo
{
public:
Foo() { x = new int; }
~Foo() { delete x; }
private:
int *x;
};
Foo *p = new Foo[10];
free ( p );
Run Code Online (Sandbox Code Playgroud)
我对上面的代码感到困惑.它有什么问题吗?
可能重复:
删除NULL指针是否安全?
在我的头文件中,我声明了一个类范围内的变量:
FaultModel<double> *fm_req_set_odom_px;
Run Code Online (Sandbox Code Playgroud)
...在类构造函数中有条件地初始化,具体取决于配置文件的值:
const char *configModel = ConfigReader->ReadString("FaultModel");
if (strcmp(configModel, "cyclic") == 0)
fm_req_set_odom_px = new CyclicFaultModel<double>();
Run Code Online (Sandbox Code Playgroud)
我的问题是:我是否需要用条件包装删除来检查模型是否已初始化,或者在任何一种情况下都可以安全删除它?
if (fm_req_set_odom_px != NULL) // Is this necessary?
delete fm_req_set_odom_px;
Run Code Online (Sandbox Code Playgroud) 我有一个包含几个向量的类,我不确定哪种方法是最好的,但是当我调用析构函数时,它们应该从内存中删除.
标头:
class Test
{
public:
Test();
~Test();
void AddString(char* text);
void AddString(string text);
private:
char * StringToCharPointer(string value);
vector<char*> *pVector;
}
Run Code Online (Sandbox Code Playgroud)
CPP文件:
Test::Test()
{
};
Test::~Test()
{
vector<char*>::iterator i;
for ( i = pVector->begin() ; i < pVector->end(); i++ )
{
delete * i;
}
delete pVector;
};
char * Test::StringToCharPointer(string value)
{
char *pChar = new char[value.length()];
strcpy(pChar, value.c_str());
return pChar;
};
Test::AddString(char* text)
{
pVector->push_back(text);
};
Test::AddString(string text)
{
pVector->push_back(StringToCharPointer(text));
};
Run Code Online (Sandbox Code Playgroud)
所以这里几乎是我使用的所有方法,但有什么不对?
我是C++的新手,从我到目前为止学到的东西,当你在一个指向堆上创建的东西的指针上调用delete时,该指针所指向的任何内容都会被擦除并释放内存,对吧?
但是当我在一个简单的类上尝试这个时:
class MyClass
{
int _Id;
public:
MyClass(int id) : _Id(id)
{
std::cout << "$Constructing the damn thing! " << _Id << std::endl;
}
~MyClass()
{
std::cout << "?Destructing the damn thing! " << _Id << std::endl;
}
void Go_XXX_Your_Self()
{
std::cout << "%OooooooooO NOOOOOO! " << _Id << std::endl;
delete this;
}
void Identify_Your_Self()
{
std::cout << "#Object number: " << _Id << " Located at: " << this << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
这些只是一些愚蠢的测试,看看删除如何工作:
int main()
{
MyClass* …Run Code Online (Sandbox Code Playgroud) 我对C++中的内存解除分配感到困惑.我有一个结构
struct Node{
Node* left;
Node* right;
};
Run Code Online (Sandbox Code Playgroud)
我将Node类型的指针声明为:
struct Node* myNode = new Node;
现在,如果我这样delete myNode做,它还取消分配left和right指针内部myNode?如果没有,如果我们在Node类型内部有很多指针并且在没有内存泄漏的情况下编写代码将会非常困难,那会不会非常繁琐?
我有一个类型,作为成员指向另一个对象的指针.创建对象(Demo)时,它会初始化构造函数中的另一个对象(Property).我想如果我有很多这些对象,当我将属性更改为不同的东西时,我不想浪费堆空间,所以我释放了初始化的内存setProperty,然后在Demo的deconstrucor中我再次删除它,因为它会有在那一点上被设定为不同的东西.但是,这会导致代码崩溃.如果我在property重置时重新分配,为什么会崩溃?
class Property{ };
class Demo{
protected:
Property *property;
public:
Demo(){ property = new Property();};
void setProperty(Property *p){
delete property;
property = p;
};
~Demo(){
delete property;
};
};
int main(){
Property p = Property();
Demo * d = new Demo();
d->setProperty(&p);
delete d;
}
Run Code Online (Sandbox Code Playgroud) 我刚刚开始学习C++,我很难理解delete []运算符的工作原理.我编写了一个代码来查看它是如何工作的,但它只删除了数组的前2个元素.我的问题是如何删除数组?
这是代码:
#include <iostream>
int main()
{
using namespace std;
int size;
cout << "Size: ";
cin >> size;
int * pt = new int [size];
for(int i = 0; i < size; i++) {
pt[i] = i + 1;
cout << "Addrres: " << &pt[i] << " " << "Index: " << i << " "
<< "Value: " << pt[i] << " " << endl;
}
cout << "----------DELETING----------" << endl;
delete [] pt;
for(int i = …Run Code Online (Sandbox Code Playgroud) c++ ×10
delete-operator ×10
pointers ×3
destructor ×2
new-operator ×2
arrays ×1
class ×1
free ×1
function ×1
member ×1
memory-leaks ×1
object ×1
oop ×1
valgrind ×1
vector ×1