Thread: strange error...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    42

    strange error...

    well i am writing the game battleship..FINALLY have it compiling w/o initial errors...everything works right up to a point...

    when i fire a shot and its a miss, it works well, but if i fire a shot and its a hit it brings up this error ...

    unhandled exception at 0x0044925f in battleship.exe: 0xC0000005: Access Violation writing location 0x00130888

    in that screen it asks to break or continue...so i hit break..and can't figure out whats wrong..

    any help with this error would be appreciated...

    BBNERD

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Not much we can do if you don't post the code.

    Don't worry, we won't steal it.

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Or so we want u to think muwahaha

    anyway, did u run the debugger?

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, well post the code that is responsible for handling a hit... are you using pointers there at all? If so, I suggest you check it.

    P.S. the dreaded "access violation" haunts everyone, even M$, if you use their programs enough to come across one
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    well in main, if theres a hit i do this in main...

    else if (field.check_shot(row-1, col-1))
    {
    graphic(cout, 'H');
    index = int(field.get_cell(row-1, col-1)) - 65;
    field.change_grid(row-1, col-1, '*');
    play.change_grid(row-1, col-1, 'H');
    ship_arry[index].inc_hits(play);
    system ("cls");
    play.print_grid(cout);
    field.add_hit();
    }
    ---------------------------------------------------------------------------

    **field and play are objects of a class called ocean
    **ship_arry is an array declares in the class called ship

    i will post all the functions now...

    -----------------------------------------------------------------------------
    graphic function is just a function in main that displays HIT or MISS
    as a stupid graphic

    ----------------------------------------------------------------------------
    index = int(field.get_cell(row-1, col-1)) - 65;

    this code finds the cell by this function and subtracts 65 on it and returns an integer value. One the field, the ships on the field are lettered A, B, C, D, etc...this uses ACSII, so if i want to find what ship is hit, I find the row, subtract 65, get a letter, etc. A on the ASCII chart is 65 i believe..anyways this is code that our instructor gave us..i can't change that...

    ------------------------------------------------------------------------

    field.change_grid(row-1, col-1, '*');
    play.change_grid(row-1, col-1, 'H');

    the function- change_grid {grid[row][col] = symbol}
    all it does is assign a certain symbol, in this case "H" to the actual grid that is displayed to the user
    ------------------------------------------------------------------------------
    ship_arry[index].inc_hits(play);

    This is the function where the error breaks...

    it looks like this...

    void ship::inc_hits(ocean& play)
    {

    num_hits++;
    change_status();

    if (status == 's')

    sink_ship(play);
    }

    void ship::change_status()
    {
    if (status == 'c')
    {
    status = 'i';
    }
    else if (num_hits == length && status == 'i')
    {
    status = 's';//sunk
    }
    }

    void ship::sink_ship(ocean & play)
    {
    report_sunk();
    for (int l = 0; l < length; l++)
    play.change_grid(place_arry[l].row, place_arry[l].col, 'S');
    }

    --------------------------------------------------------------------------

    All the report_sunk function does is display a graphic

    ------------------------------------------------------------------------------

    and print_grid simply prints the updated grid

    ---------------------------------------------------------------------

    this sucks..lol i'm sorry if its confusing..thanks again..

    BBNERD

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    god there isn't something right....now even if i know i hit one, it still drops into a miss...

    this sucks...

    PS..i'm not using a pointer that i declared myself

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>ship_arry[index].inc_hits(play);
    Determine the value of index before you use it in this statement. Then check to see that it is within the array bounds. If it's not, you'll crash.

    >> this is code that our instructor gave us
    I feel for you

    [edit]And please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    else if (field.check_shot(row-1, col-1))
    {
    graphic(cout, 'H');
    index = int(field.get_cell(row-1, col-1)) - 65;
    field.change_grid(row-1, col-1, '*');
    play.change_grid(row-1, col-1, 'H');
    ship_arry[index].inc_hits(play);
    system ("cls");
    play.print_grid(cout);
    field.add_hit();
    }
    I'm almost sure the ship_arry line is the one giving you trouble... it's not the function, I don't think, but the "ship_arry[index]" part. Does it matter where on the ship you hit, or what ship you hit? It sounds like you're trying to access a ship that's not there...

    P.S. listen to hammer, he probably knows better than I do (damn you, fast typer! )
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    i do determine the value of index...

    i use this function that the teacher gave us...


    this is how the teacher described this function to us...

    "during play, you can figure out which ship wwas hit by using this code....


    index = int(field.get_cell(row-1, col-1)) - 65;

    where index is declared as an integer; field is the ocean object that holds the actual ship placement, and row and col are the users choices of which row and column to send the missile launch. This line gets a character from the placement ocean, coverts ints acsii value to an integer and subtracts 65 to get the equivalent digit v alue that would correspond to the correct index
    of the ocean grid array. This is depends on the code that was prived to you and would not work in other circumstances."

    the code they gave us was that line and two helper functions that place the ship on the field.

    I dunno if this help you guys anymore..

    but keep trying!!

    BBNERD

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i do determine the value of index...
    Stop believing the words on paper, and let your computer determine it for you. Get it to prove to you that index is within the range 0 to n-1 where n is the number of elements in the array.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I agree with Hammer. Are you using MSVC++? If so, use the debugger, and set a breakpoint before that line and check to see if index is ok. It could be that the "- 65" is included in the function already, and by putting it there you are making index a negative number, which is definitely an out-of-bounds index
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    okay i see what ur saying..
    i run through it and it says index is 61 somehow

    i know thats beyond the memory of the array which is only supposed to be 10 spaces...

    but if i run to cursor or set a breakpoint on the actual function that finds index, it brings up the error i had at the beginning again...the exception one...

    i dunno...keep helpin..we'll figure this bugger out...haha

    BBNERD

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    hunter - i am using microsoft visual studio.net

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    42
    i can't be the teachers fault..LOL i think i'm doing something wrong...AHH

  15. #15
    pointing
    Guest
    that error means :

    you are trying to reach somewhere in the harddisc which you do not have access

    this means:

    either you want to reach an array index out of bounds
    or want to reach a dangling reference
    etc...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM