我遇到了一些奇怪的问题。我在类方法内部使用删除运算符,我想知道如何解决这个问题。
这是代码:
#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) 我有以下用于实现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) 我创建了一个类并重载了new和delete运算符来打印分配/释放的内存大小。在下面的示例中,它分配了 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) 请注意,问题已更改,不再与答案匹配
我正在尝试创建内存来保存浮动缓冲区(这里是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) 我有一个包含3个节点指针和2个整数的节点类.我分配了所有节点new,但是当我调用delete它们时,整数被设置为-17891602并且它会搞砸我代码的其余边界检查.会导致delete这样做的原因是什么?
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) 我写了一个简单的程序:
#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流....
它只是崩溃..
我无法找到原因..
当我运行以下代码时:
#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) 我正在尝试在堆上创建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 ++的新手,遇到了删除指向数组的指针的问题.如果你搜索"如何正确删除指向数组的指针",之前有一个类似的问题.结论是,保持使用new和使用的习惯是一个好习惯delete.如果您使用new <data-type> []分配momery,delete[]应该遵循.
我对这个答案不满意.计算机在执行delete或delete[]执行时会做什么?所以,我试过这个.
#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++ ×10
delete-operator ×10
new-operator ×3
arrays ×2
class ×2
c++11 ×1
garbage ×1
object ×1
pointers ×1