getting many errors from these lines of code:

-1

this is the main.cpp file, everything seems to work fine until I hit the "Arr = ship1.retrieve()" function call.

#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;
}

here is the retrieve member function called above:

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;
    }

and these are the errors/exceptions I got:

'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).

it is also worth mentioning some errors poped up after the functional call, this seems to be a memory allocation problem(I am relatively new to c++).

here is what is left of the class(I know its poorly coded 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(){
    }


};

If someone can explain why this is happening to me I would be forever grateful. thanks in advice for any replies!

c++
asked on Stack Overflow Jan 28, 2015 by Matias Chara

1 Answer

0

One thing I see is your class member int bArray[80][24] is being iterated through loops:

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

The problem with this is you're iterating through the loops 81 and 25 times respectively due to the <= operands. This will index outside of your double array.

EDIT: This also means x is being iterated too many times considering the line:

int *p = new int[1920];

answered on Stack Overflow Jan 28, 2015 by PDizzle745

User contributions licensed under CC BY-SA 3.0