[英]How to deallocate array of class instances in C++?
I am making a little game, I store the map in an array, for a reason I think is needless to inform you about my map is not just a char
array, I have made a class:我正在制作一个小游戏,我将地图存储在一个数组中,出于某种原因,我认为不必告诉您我的地图不仅仅是一个char
数组,我已经创建了一个类:
enum Marks { N, Ex3, Ex2, Ex1, Pl, En, Pb, Eb};
class MC /*Map Component*/{
public:
Marks Marked = Marks::N;
char ch = '\0';
};
and my array is an array of instances of this class, but bcs, according to Visual Studio, my map is to big for the stack, I made heap allocation, MC* map = new MC[1050]; // 15 * 70
并且我的数组是此类的实例数组,但是 bcs,根据 Visual Studio,我的地图对于堆栈来说太大了,我进行了堆分配, MC* map = new MC[1050]; // 15 * 70
MC* map = new MC[1050]; // 15 * 70
Everything works fine, until the player dies, the game ends and the time has come for the map to be deleted, delete[] map
throws Invalid address specified to RtlValidateHeap([some hexadecimal number, an address probably], [some hexadecimal number, an address probably])
一切正常,直到玩家死亡,游戏结束, delete[] map
地图的时间到了, delete[] map
throws Invalid address specified to RtlValidateHeap([some hexadecimal number, an address probably], [some hexadecimal number, an address probably])
I know I can use vector
s, but I just dont want, I want to learn more about memory managment, get more familiar with pointers etc我知道我可以使用vector
s,但我只是不想,我想了解更多关于内存管理的知识,更熟悉指针等
Edit: Visual Studio
also opens a file named delete_scalar.cpp
.编辑: Visual Studio
还会打开一个名为delete_scalar.cpp
的文件。 Code of that file:该文件的代码:
//
// delete_scalar.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the scalar operator delete.
//
#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>
////////////////////////////////////////////////////////////////
// delete() Fallback Ordering
//
// +-------------+
// |delete_scalar<----+-----------------------+
// +--^----------+ | |
// | | |
// +--+---------+ +--+---------------+ +----+----------------+
// |delete_array| |delete_scalar_size| |delete_scalar_nothrow|
// +--^----^----+ +------------------+ +---------------------+
// | |
// | +-------------------+
// | |
// +--+--------------+ +------+-------------+
// |delete_array_size| |delete_array_nothrow|
// +-----------------+ +--------------------+
_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
#ifdef _DEBUG
_free_dbg(block, _UNKNOWN_BLOCK);
#else
free(block);
#endif
}
And after some tests I found out that the program crashes after delete[]
, what I mean:经过一些测试,我发现程序在delete[]
后崩溃,我的意思是:
//
// code that runs the game
//
std::cout << endl << "Before delete";
_getch();
delete[] map;
std::cout << endl << "After delete";
_getch();
return; //function is void and is not main()
Output:输出:
Before delete
//me pressing something
After delete
//me pressing something
//program crashes
Apology: bcs Visual Studio
opened delete_scalar.cpp
I assumed that delete[]
was causing the program to crash, now I just dont know, I am confused.道歉:bcs Visual Studio
打开了delete_scalar.cpp
我以为是delete[]
导致程序崩溃,现在我不知道,我很困惑。
Well, I am terribly sorry, bcs it turns out that I am a total idiot, my stupidness knows no bounds.好吧,我非常抱歉,事实证明我是一个彻头彻尾的白痴,我的愚蠢是无止境的。 The reason I am saying this: as the game runs certain type of objects get created, deleted and recreated all the time and bcs of that I was allocating them to the heap, but I forgot to make my program delete
them in case they were "alive" at the moment the player dies, I cant believe how much of an idiot I am to spend 3 days on something like this.我这么说的原因是:当游戏运行时,某些类型的对象一直被创建、删除和重新创建,并且我将它们分配到堆中,但我忘记让我的程序delete
它们,以防它们“活着”在玩家死亡的那一刻,我简直不敢相信我是个多么愚蠢的人,要花 3 天时间做这样的事情。
Thank you to everyone trying to solve my problem even if they had not the needed information, I didn't knew exactly what information was needed so...yeah, I am sorry感谢所有试图解决我的问题的人,即使他们没有所需的信息,我也不知道到底需要什么信息,所以......是的,我很抱歉
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.