标签: delete-operator

删除类内部

我遇到了一些奇怪的问题。我在类方法内部使用删除运算符,我想知道如何解决这个问题。

这是代码:

#include <iostream>

using namespace std;

class A
{
    public:
        int a;

        ~A() {
            cout << "call ~A()" << endl;
        }

        void action()
        {
            /* code here */
            delete this; //this line depends on some if statements
            /* code here */
        }

        void test2() {
            cout << "call test2()" <<  a << endl;
        }

        void test() {
            a = 10;
            cout << "call test()" << endl;

            action();

            //how can I check if data is deleted?
            test2();
        }
};

int …
Run Code Online (Sandbox Code Playgroud)

c++ class delete-operator

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

如何在析构函数中使用delete[]

我有以下用于实现Bucket课程的文件。但是我无法销毁s 生成的_str析构函数中的成员。我得到的错误是:Bucketoperator+

正常块后检测到堆损坏...CRT 检测到应用程序在堆缓冲区末尾后写入内存

添加两个Bucket对象后总是会出现问题,但是我在调​​试过程中检查了生成的字符串以及长度是否正确。

另外,如何访问函数_len中的属性operator>>以便分配新值?

头文件:

class Bucket
{
    friend ostream& operator<<(ostream& os, const Bucket& c);
    friend istream& operator>>(istream& is, const Bucket& c);
public:
    Bucket();
    Bucket(const String& str);
    Bucket(const char* str);
    ~Bucket();
    const Bucket operator+(const Bucket& s) const;
    const Bucket operator+(const char* s) const;
    const Bucket& operator=(const char* c);
    const bool operator==(const char* str);
private:
    char* _str;
    int _len;
}; 
Run Code Online (Sandbox Code Playgroud)

源文件:

Bucket::Bucket() {
    _len = 0;
    _str …
Run Code Online (Sandbox Code Playgroud)

c++ delete-operator

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

为什么重载new和delete运算符时释放的内存小于分配的内存?

我创建了一个类并重载了newdelete运算符来打印分配/释放的内存大小。在下面的示例中,它分配了 28 个字节,但释放了 4 个字节。为什么?

#include <iostream>
#include <string>
using namespace std;

class Person
{
private:
    string name;
    int age;

public:
    Person() {
        cout << "Constructor is called.\n";
    }

    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }

    void* operator new(size_t size) {
        void* p = malloc(size);
        cout << "Allocate " << size << " bytes.\n";
        return p;
    }

    void operator delete(void* p) {
        free(p);
        cout << "Free " << sizeof(p) << " …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading new-operator delete-operator

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

指针外部内存分配时如何使用delete []?

请注意,问题已更改,不再与答案匹配

我正在尝试创建内存来保存浮动缓冲区(这里是4个浮点数).我已经分配了内存,内存中的所有4个值都是零.

当然循环迭代4次,但是第4次将ptr移动到我分配的内存之外.所以在循环结束时我将ptr移回我分配内存的地方,然后使用delete[].

我的问题是:我打电话时是否删除了整个4浮点缓冲区delete[]?(这显然是我需要的!)

int inFramesToProcess = 4;
float *ptr = new float[inFramesToProcess]();

    for(UInt32 i = 0; i < inFramesToProcess; ++i) {
        ptr++;
    }

    ptr -= inFramesToProcess;

delete[] ptr; 
Run Code Online (Sandbox Code Playgroud)

c++ pointers segmentation-fault delete-operator

-1
推荐指数
2
解决办法
1047
查看次数

什么会导致删除在我的类的数据字段中留下垃圾值?

我有一个包含3个节点指针和2个整数的节点类.我分配了所有节点new,但是当我调用delete它们时,整数被设置为-17891602并且它会搞砸我代码的其余边界检查.会导致delete这样做的原因是什么?

c++ class garbage delete-operator

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

析构函数删除数组时的错误

class Celda
{
private:
    int* bloques_;
public:
    Celda(int size);
    ~Celda(){ delete [] bloques_; };
};
Celda :: Celda(int size)
{
    bloques_ = new int[size];
    bloq_ocupados_ = 0;
    tam_ = size;
    for(int i = 0 ; i < tam_ ; ++i){
        bloques_[i] = 0;
    }
}

Tabla :: Tabla(int numcel, int tambloq)
{
    nceldas_ = numcel; tam_bloque_ = tambloq;
    tabla_ = new Celda*[nceldas_];
    for(int i = 0 ; i < nceldas_ ; ++i){
        tabla_[i] = new Celda(tam_bloque_);
}
    ocupadas_ = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ arrays delete-operator

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

删除在不同函数中动态分配的对象时发生崩溃

我写了一个简单的程序:

#include<iostream>
#include<list>
using namespace std;
list<int>& func();

int main(){
    list<int> a = func();
    delete &a;
    std::cout<<"Here\n";
}

list<int>& func(){
    list<int>* ptr = new list<int>;
    return *ptr;
}
Run Code Online (Sandbox Code Playgroud)

这个程序永远不会打印Here到cout流....

它只是崩溃..

我无法找到原因..

c++ new-operator delete-operator heap-corruption

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

C++数组:为什么delete []不起作用?

当我运行以下代码时:

#include <iostream>
using namespace std;

main(){
  //declare:
    char* TestArray = new char[16];
    for(char i=0; i<16; i++){
            TestArray[i]=rand() % 10;
    }
  //output Array:
    for(char i=0; i<16; i++){
            cout << int(TestArray[i]) << " ";
    }
  //delete it:
    delete[] TestArray;
  //output result:
    for(char i=0; i<16; i++){
            cout << int(TestArray[i]) << " ";
    }
Run Code Online (Sandbox Code Playgroud)

结果是:

3 6 7 5 3 5 6 2 9 1 2 7 0 9 3 6 //declared Array
0 0 0 0 0 0 0 0 9 1 …
Run Code Online (Sandbox Code Playgroud)

c++ arrays delete-operator

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

为什么删除忽略删除除第一个之外的arrary中的对象?

我正在尝试在堆上创建4个Student对象.当我尝试删除它们时,只删除第一个.

#include <iostream>
using namespace std;
class Student{
private:
    int ID;
    int score;
public:
    void setID(int num);
    int getID();
    void setScore(int num);
    int getScore();
};
void Student::setID(int num)
{
    ID = num;
}
int Student::getID()
{
    return ID;
}
void Student::setScore(int num)
{
    score = num;
}
int Student::getScore()
{
    return score;
}
class Creator
{
public:
    static int nextID;
    Student* getObject();
};
int Creator::nextID = 0;
Student* Creator::getObject()
{
    Creator::nextID++;
    Student* temp = new Student();
    temp->setID(Creator::nextID);
    return temp;
} …
Run Code Online (Sandbox Code Playgroud)

c++ object new-operator delete-operator

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

删除或删除[]时计算机会执行什么操作?

那里!我是c ++的新手,遇到了删除指向数组的指针的问题.如果你搜索"如何正确删除指向数组的指针",之前有一个类似的问题.结论是,保持使用new和使用的习惯是一个好习惯delete.如果您使用new <data-type> []分配momery,delete[]应该遵循.

我对这个答案不满意.计算机在执行deletedelete[]执行时会做什么?所以,我试过这个.

#include <iostream>
using namespace std;

int main()
{
    int* pvalue = NULL;
    pvalue = new int[3]; 
    cout << "address of pvalue:" << pvalue << endl;

    for (int i = 0; i < 3; ++i)
    {
        *(pvalue+i) = i+1;
    }
    for (int i = 0; i < 3; ++i)
    {
        cout << "Value of pvalue : " << *(pvalue+i) << endl;
    }

delete pvalue; …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-memory-allocation delete-operator c++11

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