C++ 接收0xC0000005:访问冲突读取位置在指针数组中存储新指针

C++ 接收0xC0000005:访问冲突读取位置在指针数组中存储新指针,c++,pointers,graphics,logic,sfml,C++,Pointers,Graphics,Logic,Sfml,我正在使用SFML图形库编写一个基本的Asteroids程序,调试程序时收到错误/崩溃。只有当我试图通过空格键向我的飞船发射“光子鱼雷”时才会发生这种情况。下面是一段代码 //check for keypress if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) { for (int index = 0; index <

我正在使用
SFML图形库编写一个基本的
Asteroids
程序,调试程序时收到
错误/崩溃
。只有当我试图通过空格键向我的飞船发射“光子鱼雷”时才会发生这种情况。下面是一段代码

//check for keypress

        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
            for (int index = 0; index < MAX_PHOTONS; index++) {
                photons[index] = new SpaceObject(PHOTON_TORPEDO, PHOTON_RADIUS, ship->getLocation(), ship->getVelocity(), ship->getAngle())
                photons[index]->applyThrust(PHOTON_SPEED);
                std::cout << "LAUNCHING TORPEDO\n";
            }
        }

    // update game objects ------------------------------------------
     ship->updatePosition();

     for (int index = 0; index < ARRAY_SIZE; index++) {
         if (&asteroids[index] != NULL) {
             asteroids[index]->updatePosition();
         }
     }

     for (int index = 0; index < MAX_PHOTONS; index++) {
         if (&photons[index] != NULL) {
             photons[index]->updatePosition();
         }
     }


    // draw new frame ------------------------------------------------
    window.clear();
    window.draw(background);

    for (int index = 0; index < ARRAY_SIZE; index++) {
        if (&asteroids[index] != NULL) {
            asteroids[index]->draw(window);
        }
    }

    for (int index = 0; index < MAX_PHOTONS; index++) {
        if (&photons[index] != NULL) {
            photons[index]->draw(window);
        }
    }
//检查是否有按键
if(event.type==sf::event::KeyPressed&&event.key.code==sf::Keyboard::Space){
对于(int index=0;index获取位置(),飞船->获取速度(),飞船->获取角度())
光子[指数]->应用信任(光子速度);
std::cout updatePosition();
对于(int index=0;indexupdatePosition();
}
}
对于(int index=0;indexupdatePosition();
}
}
//画新框架------------------------------------------------
window.clear();
窗口。绘制(背景);
对于(int index=0;index绘制(窗口);
}
}
对于(int index=0;index绘制(窗口);
}
}
运行代码会导致即时崩溃,调试会导致:

polygons2.exe中0x00311746处的未处理异常:0xC0000005:Access 读取位置0x00000018时发生冲突


我认为错误在于按键事件。

您的代码有输入错误。不要使用引用操作符访问对象引用。小行星和光子的元素是相应对象的地址

 for (int index = 0; index < ARRAY_SIZE; index++) {
     if (asteroids[index] != NULL) {
         asteroids[index]->updatePosition();
     }
 }

 for (int index = 0; index < MAX_PHOTONS; index++) {
     if (photons[index] != NULL) {
         photons[index]->updatePosition();
     }
 }


// draw new frame ------------------------------------------------
window.clear();
window.draw(background);

for (int index = 0; index < ARRAY_SIZE; index++) {
    if (asteroids[index] != NULL) {
        asteroids[index]->draw(window);
    }
}

for (int index = 0; index < MAX_PHOTONS; index++) {
    if (photons[index] != NULL) {
        photons[index]->draw(window);
    }
}
for(int index=0;indexupdatePosition();
}
}
对于(int index=0;indexupdatePosition();
}
}
//画新框架------------------------------------------------
window.clear();
窗口。绘制(背景);
对于(int index=0;index绘制(窗口);
}
}
对于(int index=0;index绘制(窗口);
}
}

仅为澄清Dmitry Kolchev的正确答案:

在代码中使用
&
时,会检索实体的地址:

int i = 15;   /* Integer, contains '15' */
int* pi = &i;  /* Pointer to an integer, contains the address of i in memory*/
现在有了一个类似

int array [3] = {1, 2, 3};
然后

坚持住。
array[i]
检索位于
i
位置的数组内容。
&array[i]
表示位于
i
位置的元素的内存地址:

int a0 = array[0];   /* array[0] is of type int */
int* ap0 = &array[0]; /* &array[0] is of type int* */
assert(array[i] == *(array + i));
assert(&array[i] == array + i);
顺便说一下,
array[i]
只是
*(array+i)
的缩写,而
&array[0]
等于
array+i

assert(array[i] == *(array + i));
assert(&array[i] == array + i);

但这是一个稍有不同的情况…

如果它导致即时崩溃,您有一个成熟的调试器候选者。访问冲突强烈表明您正在取消对某个结构的空指针的引用,特别是该结构上24字节深的某个结构。使用调试器,您会发现问题。仅供参考,此:
如果(&asteroids[index]==NULL)
毫无意义。该符号不应该存在。该错误在该代码的多个位置重复。
光子[index]!=NULL
始终为真。