问题描述
我正在做一个小游戏,我将地图存储在一个数组中,出于某种原因,我认为无需通知您我的地图不仅仅是一个char
数组,我做了一个类:>
enum Marks { N,Ex3,Ex2,Ex1,Pl,En,Pb,Eb};
class MC /*Map Component*/{
public:
Marks Marked = Marks::N;
char ch = '\0';
};
我的数组是此类的实例数组,但是bcs,根据Visual Studio,我的映射对于堆栈来说很大,我进行了堆分配,MC* map = new MC[1050]; // 15 * 70
一切正常,直到玩家死亡,游戏结束并且删除地图的时间到了,delete[] map
抛出Invalid address specified to RtlValidateHeap([some hexadecimal number,an address probably],[some hexadecimal number,an address probably])
我知道我可以使用vector
,但我只是不想,我想了解有关内存管理的更多信息,更熟悉指针等
编辑:Visual Studio
还会打开一个名为delete_scalar.cpp
的文件。
该文件的代码:
//
// 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
}
经过一些测试,我发现程序在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()
输出:
Before delete
//me pressing something
After delete
//me pressing something
//program crashes
道歉:bcs Visual Studio
打开了delete_scalar.cpp
,我以为delete[]
导致程序崩溃,现在我不知道,我很困惑。
解决方法
好吧,我非常抱歉,因为事实证明我是个白痴,我的愚蠢无止境。我这么说的原因是:随着游戏的运行,某些类型的对象会一直被创建,删除和重新创建,而bcs却一直在将它们分配到堆中,但是我忘记将程序delete
放在其中如果他们在玩家死亡的那一刻“还活着”,我不敢相信我会花3天的时间在这样的事情上呆多少白痴。
谢谢所有试图解决我的问题的人,即使他们没有所需的信息,我也不知道到底需要什么信息,所以...是的,很抱歉