从这些代码行中获取许多错误:

时间:2015-01-28 19:47:57

标签: c++

这是main.cpp文件,一切似乎工作正常,直到我点击" Arr = ship1.retrieve()"功能调用。

#include<iostream>
#include<string>
#include"ship.h" 
using namespace std;


int main(){
    int newarray[5][5] = {
        0, 0, 1, 0, 0, 
        0, 1, 1, 1, 0,
        1, 1, 1, 1, 1,
        0, 1, 1, 1, 0,
        0, 0, 1, 0, 0
    };
    int arr[80][24];
    int *Arr;
    ship ship1(newarray, 1);
    ship1.spawn(40, 12);
    Arr = ship1.retrieve();
    system("pause");
    delete Arr;
}

这是上面调用的检索成员函数:

int *retrieve(){
        int *p = new int[1920];
        for (int i = 0,x = 0; i <= 80; i++, x++)
        {
            for (int o = 0; o <= 24; o++, x++)
            {
                p[x] = bArray[i][o];
            }
        }
        return p;
    }

这些是我得到的错误/例外:

'text-based game engine.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'text-based game engine.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'text-based game engine.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'text-based game engine.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Cannot find or open the PDB file.
'text-based game engine.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file.
Critical error detected c0000374
text-based game engine.exe has triggered a breakpoint.

First-chance exception at 0x7774EBD8 (ntdll.dll) in text-based game engine.exe: 0xC0000374: Se produjeron daños en un montón (parameters: 0x7776C338).
Unhandled exception at 0x7774EBD8 (ntdll.dll) in text-based game engine.exe: 0xC0000374: Se produjeron daños en un montón (parameters: 0x7776C338).

HEAP[text-based game engine.exe]: Heap block at 0052A958 modified at 0052C784 past requested size of 1e24
text-based game engine.exe has triggered a breakpoint.

HEAP[text-based game engine.exe]: Invalid address specified to RtlValidateHeap( 00520000, 0052A960 )
text-based game engine.exe has triggered a breakpoint.

First-chance exception at 0x776AE622 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
HEAP[text-based game engine.exe]: Heap block at 0052A958 modified at 0052C784 past requested size of 1e24
text-based game engine.exe has triggered a breakpoint.

HEAP[text-based game engine.exe]: Invalid address specified to RtlFreeHeap( 00520000, 0052A960 )
text-based game engine.exe has triggered a breakpoint.

First-chance exception at 0x776AD800 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776B4F72 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD.
First-chance exception at 0x776AD800 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AD800 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AE622 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AD800 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AE622 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776B4F72 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD.
First-chance exception at 0x776AE622 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AD800 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AE622 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AD800 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AE622 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AD800 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
First-chance exception at 0x776AE622 (ntdll.dll) in text-based game engine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
The program '[15204] text-based game engine.exe' has exited with code -1073741510 (0xc000013a).

还值得一提的是在函数调用之后出现了一些错误,这似乎是一个内存分配问题(我对c ++来说相对较新)。

这是课程的剩余部分(我知道编码很差的D :):

ship.h

    #pragma once
    class ship
    {
    private:
        int mainArray[5][5];
        const int thisId;
        int y;
        int x;
        int bArray[80][24];
        bool spawnCap;
        void replaceAll(const int replaceVal = 0){
            for (int i = 0; i <= 5; i++){
                for (int o = 0; o <= 5; o++){
                    //finds and replaces the default identifiers in the user created array (1= block, 0= air/nothing) with identifiers reconginazable by the class function members (replaces the default block id to the id used by the class instance(object)) 
                    if (find(i, o)){
                        mainArray[i][o] = (replaceVal > thisId)?replaceVal:thisId;
                    }
                }
            }
        }
    public:
        bool check(int aArray[80][24]){
            int tempArray[5][5];

            for (int i = 0, i2 = y; i <= 5; i++, i2++)
            {
                for (int o = 0, o2 = x; o <= 5; o++, o2++)
                {
                    //crops a specific area (5*5) and saves it to tempArray
                    tempArray[i][o] = aArray[i2][o2];
                }
            }


            for (int i = 0; i <= 5; i++){
                for (int o = 0; o <= 5; o++){
                    //compares the main array to a specific area in the buffer array, returns true if arrays dont coincede(useful for colision detecting)
                    if (tempArray[i][o] != mainArray[i][o] && tempArray[i][o] != 0){
                        return true;
                    }
                }
            }
            return false;
        }

        void spawn(int Y, int X){
            if (!spawnCap){
                y = Y;
                x = X;
                spawnCap = true;
                for (int i = 0, i2 = y; i <= 5; i++, i2++)
                {
                    for (int o = 0, o2 = x; o <= 5; o++, o2++)
                    {
                        //copies the user created "ship" into a determined area in the buffer (can only be done once per object)
                        bArray[i2][o2] = mainArray[i][o];
                    }
                }
            }
        }

        void move(int Y, int X){
            if (spawnCap){
                for (int i = y; i <= 5; i++)
                {
                    for (int o = x; o <= 5; o++)
                    {
                        //delete last position or first (spawn) position
                        bArray[i][o] = 0;
                    }
                }

                y = Y;
                x = X;

                for (int i = 0, i2 = y; i <= 5; i++, i2++)
                {
                    for (int o = 0, o2 = x; o <= 5; o++, o2++)
                    {
                        //copies the user created "ship" into a determined area in the buffer
                        bArray[i2][o2] = mainArray[i][o];
                    }
                }

            }
        }

        bool find(int arrayCoorX, int arrayCoorY, int valueToFind = 1){
            if (mainArray[arrayCoorX][arrayCoorY] == valueToFind){
                return true;
            }
            else{
                return false;
            }
        }

        void update(int BArray[80][24]){
            for (int i = 0; i <= 80; i++)
            {
                for (int o = 0; o <= 24; o++)
                {
                    bArray[i][o] = BArray[i][o];
                }
            }
        }

        int *retrieve(){
            int *p = new int[1920];
            int x = 0;
            for (int i = 0; i <= 80; i++, x++)
            {
                for (int o = 0; o <= 24; o++, x++)
                {
                    p[x] = bArray[i][o];
                }
            }
            return p;
        }

        ship(int tempArray[][5], int id, int replaceValOverride = 0) :
            thisId(id)
        {
            //takes in a user created array and copies it into the class,it also takes in an id which is by default the last id + 1, the id used for the blocks can be overriden(not recomended because of the lack of support)
            for (int i = 0;i <= 5; i++)
            {
                for (int o = 0; o <= 5; o++)
                {
                    mainArray[i][o] = tempArray[i][o];
                }
            }
            replaceAll(replaceValOverride);


}

    ~ship(){
    }


};

如果有人能解释为什么会发生这种情况,我将永远感激不尽。感谢任何回复的建议!

1 个答案:

答案 0 :(得分:0)

我看到的一件事是你的班级成员int bArray[80][24]正在通过循环进行迭代:

for (int i = 0,x = 0; i <= 80; i++, x++)
{
    for (int o = 0; o <= 24; o++, x++)
    {
        p[x] = bArray[i][o];
    }
}

这个问题是你因为&lt; =操作数而分别遍历循环81和25次。这将在双数组之外进行索引。

编辑:这也意味着考虑到以下行,x被迭代次数太多次了:

int *p = new int[1920];