接收到 0xC0000005:访问冲突读取位置在指针数组中存储新指针
Recieving 0xC0000005: Access violation reading location storing new pointer in pointer array
我正在编写一个基本的 Asteroids
程序,使用 SFML graphics library
,并且在调试程序时收到 errors/crashes
。只有当我试图通过空格键将 "photon torpedo"
发射出我的船时,才会发生这种情况。这是代码片段。
//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);
}
}
运行 代码导致即时崩溃,调试结果:
Unhandled exception at 0x00311746 in polygons2.exe: 0xC0000005: Access
violation reading location 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);
}
}
特此澄清一下 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};
然后
assert(1 == array[0]);
assert(2 == array[1]);
assert(3 == array[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* */
顺便说一下,array[i]
只是 *(array + i)
的缩写,&array[0]
等于 array + i
:
assert(array[i] == *(array + i));
assert(&array[i] == array + i);
但情况略有不同...
我正在编写一个基本的 Asteroids
程序,使用 SFML graphics library
,并且在调试程序时收到 errors/crashes
。只有当我试图通过空格键将 "photon torpedo"
发射出我的船时,才会发生这种情况。这是代码片段。
//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);
}
}
运行 代码导致即时崩溃,调试结果:
Unhandled exception at 0x00311746 in polygons2.exe: 0xC0000005: Access violation reading location 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);
}
}
特此澄清一下 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};
然后
assert(1 == array[0]);
assert(2 == array[1]);
assert(3 == array[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* */
顺便说一下,array[i]
只是 *(array + i)
的缩写,&array[0]
等于 array + i
:
assert(array[i] == *(array + i));
assert(&array[i] == array + i);
但情况略有不同...