Thread: What does this error mean and how can I resolve it

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    33

    What does this error mean and how can I resolve it

    When i run my program that's supposed to copy one array to another i get the following error:
    Unhandled exception at 0x592413af (msvcr100d.dll) in TextMirrorArray.exe: 0xC0000005: Access violation reading location 0x00000026.

    What does this mean and how can i resolve this?

    Here is my code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include "ArrayMirror.h"
    #define COLLUMS 40
    #define ROWS 3
    
    void main() 
    {
        char array1[][COLLUMS] = {{"one"},{"two"},{"three"}};
        char array2[ROWS][COLLUMS];
        char *parray[ROWS][COLLUMS];
        int i,i2;
        for(i = 0;i <= ROWS; i++)
        {
            for(i2 = 0; i2 <= COLLUMS;i2++)
            {
                parray[i][i2] = &array1[i][i2];
            }
        }
        printf("array1 equals\n");
        for(i = 0;i <= ROWS; i++)
        {
            for(i2 = 0; i2 <= COLLUMS;i2++)
            {
                printf("%s",array1[i][i2]);
            }
            putchar('\n');
        }
        printf("array2 equals\n");
        for(i = 0;i <= ROWS; i++)
        {
            for(i2 = 0; i2 <= COLLUMS;i2++)
            {
                printf("%s",array2[i][i2]);
            }
            putchar('\n');
        }
        printf("Now mirroring array2 to mirror array1\n");
        MirrorArrays(array1,parray);
        printf("array2 mirrored from array 1 equals\n");
        for(i = 0;i <= ROWS; i++)
        {
            for(i2 = 0; i2 <= COLLUMS;i2++)
            {
                printf("%s",array2[i][i2]);
            }
            putchar('\n');
        }
        getchar();
    
    }
    Here is the ArrayMirror Body
    Code:
    #include "ArrayMirror.h"
    #include <stdlib.h>
    #define ROWS 20
    #define COLLUMS 40
    
    
    void MirrorArrays(char readarray[][COLLUMS], char *writearray[][COLLUMS])
    {
        int i,i2;
        for( i = 0; i <= ROWS; i++)
        {
            for( i2 = 0; i2 <= COLLUMS; i2++)
            {
                writearray[i][i2] = &readarray[i][i2];
            }
        }
        
    
    }
    And here is the ArrayMirror Header:
    Code:
    #ifndef ARRAYMIRROR
    #define ARRAYMIRROR
    
    #define COLLUMS 40
    
    void MirrorArrays(char readarray[][COLLUMS], char *writearray[][COLLUMS]); 
    
    #endif
    Thank You,

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It means your program is accessing/modifying memory it shouldn't, and the operating system is detecting that fact so it terminates your program.

    Your basic problem is with array indices. Given an array of dimension n, valid indices are 0 to n-1. With multi-dimensional arrays, that is true for every dimension.

    All of your loops go one step too far, and fall off the end of the arrays.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(i = 0;i <= ROWS; i++)
    It means that you need to learn that stepping off the end of an array is a bad thing to do.

    If you have
    int arr[N];

    Then the correct loop is
    for ( i = 0 ; i < N ; i++ )

    And NOT <=
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Didn't you just post another question with the same array index problem. What is the point of posting questions if you don't follow up by reading the comments in the answers.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    33
    Thank you for answering my question. this should work now.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Justinweq View Post
    Thank you for answering my question. this should work now.
    Only if you have a liberal interpretation of what "work" is. It may or may not crash, but your code still exhibits undefined behaviour.

    And main() returns int, not void, in C.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    The answer has already been given. But for the future -- if you run it in a debugger it'll tell you where the code has gone wrong. Can be very useful for debugging problems like this. I think gdb will just stop after the SIGSEGV, Visual Studio requires to you go to (from memory) Debug --> Processor Exceptions and tick the box for 0xC0000005: Access violation. It'll dump you in the middle of library code, but you can go back up the call stack and find out what's gone wrong.

    Wouldn't have helped here as you didn't actually know that the access was out of bounds. For the future though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot resolve error
    By Cynder in forum C Programming
    Replies: 6
    Last Post: 04-24-2012, 02:31 PM
  2. How to resolve this error in c++?
    By hr1212s in forum C++ Programming
    Replies: 4
    Last Post: 05-11-2010, 02:47 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Can't resolve linker error!!!!
    By benshums in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 02:48 PM
  5. Resolve error in Windows Macro?
    By Adock in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 08:41 PM