Deleting objects on the heap which store data on the heap

My program has been written using classes from the SDL library. I have the following class: class s_group { private: SDL_Surface* image; unsigned int* F_total; float* F_length; SDL_Rect** F; float* F_current; unsigned int S_total; unsigned int S_current; public: s_group(void); virtual ~s_group(void); bool

删除在堆上存储数据的堆中的对象

我的程序是使用SDL库中的类编写的。 我有以下课程: class s_group { private: SDL_Surface* image; unsigned int* F_total; float* F_length; SDL_Rect** F; float* F_current; unsigned int S_total; unsigned int S_current; public: s_group(void); virtual ~s_group(void); bool setup( const char* filename, unsigned int

What exactly is dynamic memory?

Possible Duplicate: Differences between dynamic memory and “ordinary” memory I was reading the C++ tutorial and I don't understand why I need to declare dynamic memory, this is what the tutorial says: Until now, in all our programs, we have only had as much memory available as we declared for our variables, having the size of all of them to be determined in the source code, before the e

什么是动态内存?

可能重复: 动态内存和“普通”内存之间的差异 我正在阅读C ++教程,我不明白为什么我需要声明动态内存,这是教程所说的: 直到现在,在我们所有的程序中,我们只有尽可能多的内存可用,就像我们为变量声明的一样,在执行程序之前,它们的大小都在源代码中确定。 然后它说我们必须使用新的和删除操作符来使用动态内存。 但是,我似乎在声明一个指针时使用动态内存,例如char * p,我没有指定字符数组的长度。 事实上,我

Destructor not deleting allocated memory

I have a class that includes a std::uint_8 pointer and the destructor should be called to delete the allocated memory. The issue I'm having is that a complier error occurs and states that the memory was not allocated, but I know I allocated it in my default constructor. Here's my default constructor: BigInteger::BigInteger() { unsigned char aArray [4]; aArray[0] = 0; m_number = n

析构函数不删除分配的内存

我有一个包含std :: uint_8指针的类,应该调用析构函数来删除分配的内存。 我遇到的问题是发生编译器错误,并指出内存未分配,但我知道我将其分配在我的默认构造函数中。 这是我的默认构造函数: BigInteger::BigInteger() { unsigned char aArray [4]; aArray[0] = 0; m_number = new unsigned char[4] m_number = aArray; m_digitCount = 0; m_sizeReserved = 4; } 这里是我的析构函数: BigInteger::~BigInte

C++ delete[] calls destructor

EDITED : added constructor and destructor. EDIT : this is the leak : c:userssijaandesktop1starray.cpp(61) : {148} normal block at 0x007C0910, 40 bytes long. Data: < h | > C8 00 00 00 02 00 00 00 68 09 7C 00 CD CD CD CD c:userssijaandesktop1starray.cpp(43) : {145} normal block at 0x007C04B0, 40 bytes long. Data: <d { > 64 00 00 00 01 00 00 00 A8 E2 7B 00 CD

C ++ delete []调用析构函数

编辑:添加构造函数和析构函数。 编辑:这是泄漏: c:userssijaandesktop1starray.cpp(61) : {148} normal block at 0x007C0910, 40 bytes long. Data: < h | > C8 00 00 00 02 00 00 00 68 09 7C 00 CD CD CD CD c:userssijaandesktop1starray.cpp(43) : {145} normal block at 0x007C04B0, 40 bytes long. Data: <d { > 64 00 00 00 01 00 00 00 A8 E2 7B 00 CD CD CD CD c:userssij

Explicitly deleting destructors and not calling delete

I was reading the C++11 FAQ and noticed this: class X4 { ~X4() = delete; // Disallow destruction } This implicitly also disallows moving of X4s. Copying is allowed, but deprecated. I also found this quote. Deleting the definition of a destructor will require allocation on the free-store because static and automatic objects implicitly invoke the destructor:` struct C { ~C()= delete

显式删除析构函数而不调用delete

我正在阅读C ++ 11 FAQ,并注意到这一点: class X4 { ~X4() = delete; // Disallow destruction } 这隐含地也禁止移动X4。 复制是允许的,但不推荐使用。 我也发现了这句话。 删除析构函数的定义将需要在free-store上进行分配,因为静态和自动对象会隐式地调用析构函数:`` struct C { ~C()= delete; //prevent automatic and static objects }; 但是,这种技术并不像看起来那么有用,因为它可以防止删除表达

Can a destructor be recursive?

Is this program well-defined, and if not, why exactly? #include <iostream> #include <new> struct X { int cnt; X (int i) : cnt(i) {} ~X() { std::cout << "destructor called, cnt=" << cnt << std::endl; if ( cnt-- > 0 ) this->X::~X(); // explicit recursive call to dtor } }; int main() { char* buf = n

一个析构函数可以递归吗?

这个程序是否定义明确,如果不是,为什么? #include <iostream> #include <new> struct X { int cnt; X (int i) : cnt(i) {} ~X() { std::cout << "destructor called, cnt=" << cnt << std::endl; if ( cnt-- > 0 ) this->X::~X(); // explicit recursive call to dtor } }; int main() { char* buf = new char[sizeof

Does calling a destructor explicitly destroy an object completely?

If I call a destructor explicitly ( myObject.~Object() ) does this assure me that the object will be appropriately destroyed (calling all child destructors) ? Ok some code: class Object { virtual ~Object() {} }; class Widget : public Object { virtual ~Widget() {} }; ... Object* aWidget = new Widget(); //allocate and construct aWidget->~Object(); //destroy and DON'T deallocate

调用析构函数是否完全破坏对象?

如果我明确地调用了一个析构函数(myObject。〜Object()),这是否会保证该对象将被适当销毁(调用所有子析构函数)? 确定一些代码: class Object { virtual ~Object() {} }; class Widget : public Object { virtual ~Widget() {} }; ... Object* aWidget = new Widget(); //allocate and construct aWidget->~Object(); //destroy and DON'T deallocate 我知道我可以删除对象,但我不想。 我希望保

on stack or heap

In the following code: class Array { public: int& operator[] (unsigned i) { if (i > 99) error(); return data[i]; } private: int data[100]; }; int main() { Array a; a[10] = 42; a[12] += a[13]; ... } (Correct me if I'm wrong) The variable a of type Array is on the stack since new was not used to allocate it. The Array class has int data[100], and the ope

在堆栈或堆上

在以下代码中: class Array { public: int& operator[] (unsigned i) { if (i > 99) error(); return data[i]; } private: int data[100]; }; int main() { Array a; a[10] = 42; a[12] += a[13]; ... } (如果我错了,纠正我)Array的变量a在堆栈中,因为没有使用new来分配它。 Array类具有int数据[100],并且运算符重载返回对数据中特定索引的引用。 引用问题。 我的问题是int

About C/C++ stack allocation

While studying C++ (and C) I had some particular doubts regarding the working of stack allocation, that I can't find a solution to: Does stack allocation call malloc/free functions implicitly? If not; how does it assure there is no conflict between stack allocation and heap allocation? If yes; does stack allocation in C++ implicitly call new/delete too? If yes; does overloading the n

关于C / C ++堆栈分配

在学习C ++(和C)时,我对堆栈分配的工作有一些特别的怀疑,我找不到解决方案: 隐式堆栈分配调用malloc / free函数吗? 如果不; 它如何确保堆栈分配和堆分配之间没有冲突? 如是; C ++中的堆栈分配是否隐式调用new / delete? 如是; 为一个类重载新运算符是否会影响其堆栈分配? 它在VC ++中产生了令人困惑的结果; 但由于VC ++不完全符合标准(或者我听说过),所以我决定最好在这里问问... 堆栈分配不使用像mal

Type of the first parameter of a member function in C++11

I have written a metafunction to retrieve the type of the first parameter of a member function, which of course receives one or more parameters. The code I have written is as follow: template <typename...> struct parameter; template < typename O, typename A, typename R, typename... Args> struct parameter <R (O::*)(A, Args...) > { using first_param = A; }; I use this me

C ++中成员函数的第一个参数的类型11

我已经写了一个元函数来检索成员函数的第一个参数的类型,该函数当然会接收一个或多个参数。 我写的代码如下: template <typename...> struct parameter; template < typename O, typename A, typename R, typename... Args> struct parameter <R (O::*)(A, Args...) > { using first_param = A; }; 我使用这个元函数如下: using mem_fn = void(mem_type::*)(std::vector<int>); using f_pm