Recieving 0xC0000005: Access violation reading location storing new pointer in pointer array

1

I am writing a basic Asteroids program, using the SFML graphics library, and am receiving errors/crashes when debugging the program. This happens only when I am attempting to fire a "photon torpedo" out of my ship, via the spacebar. Here is a snippet of the code.

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

Running the code causes an instant crash, and debugging results in:

Unhandled exception at 0x00311746 in polygons2.exe: 0xC0000005: Access violation reading location 0x00000018.

I believe the error lies in the keypress event.

c++
pointers
graphics
logic
sfml
asked on Stack Overflow May 17, 2018 by tinyJman • edited May 17, 2018 by Dee_wab

2 Answers

1

You code has typos. Don't use reference operator access object references. Elements of asteroids & photons are addresses of corresponding objects.

 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);
    }
}
answered on Stack Overflow May 17, 2018 by Dmitry Kolchev
1

Just for clarification of the correct answer of Dmitry Kolchev:

A & as you use it in your code retrieves the address of an entity:

int i = 15;   /* Integer, contains '15' */
int* pi = &i;  /* Pointer to an integer, contains the address of i in memory*/

Now you have an array like

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

Then

assert(1 == array[0]);
assert(2 == array[1]);
assert(3 == array[3]);

holds. array[i] retrieves the content of the array at position i. &array[i] denotes the memory address of the element at position i:

int a0 = array[0];   /* array[0] is of type int */
int* ap0 = &array[0]; /* &array[0] is of type int* */

By the way, array[i] is just short hand for *(array + i) and &array[0] equals array + i:

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

But that's a slightly different story...

answered on Stack Overflow May 17, 2018 by Michael Beer • edited May 17, 2018 by Michael Beer

User contributions licensed under CC BY-SA 3.0