【问题标题】:Recieving 0xC0000005: Access violation reading location storing new pointer in pointer array接收 0xC0000005:访问冲突读取位置在指针数组中存储新指针
【发布时间】:2018-10-27 06:06:56
【问题描述】:

我正在编写一个基本的Asteroids 程序,使用SFML graphics library,并在调试程序时收到errors/crashes。这只发生在我试图通过空格键从我的船上发射"photon torpedo" 时。这是代码的sn-p。

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

运行代码会导致瞬间崩溃,调试会导致:

polygons2.exe 中 0x00311746 处的未处理异常:0xC0000005:访问 违规读取位置0x00000018。

我相信错误在于按键事件。

【问题讨论】:

  • 如果它会导致即时崩溃,那么您有一个 debugger 候选者成熟。该访问冲突强烈表明您正在取消引用指向某个结构的 NULL 指针,特别是该结构上 24 字节深的东西。使用调试器,你会发现你的问题。仅供参考,这个:if (&amp;asteroids[index] == NULL) 完全没有意义。那个&符号不应该在那里。该错误在此代码中的多个位置重复出现。
  • &amp;photons[index] != NULL 始终为真。

标签: c++ pointers graphics logic sfml


【解决方案1】:

只是为了澄清 Dmitry Kolchev 的正确答案:

当您在代码中使用 &amp; 时,它会检索实体的地址:

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 的数组内容。 &amp;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) 的简写,&amp;array[0] 等于 array + i

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

但这是一个稍微不同的故事......

【讨论】:

    【解决方案2】:

    您的代码有错别字。不要使用引用运算符访问对象引用。小行星和光子的元素是对应物体的地址。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多