Thread: access violation in int array

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    access violation in int array

    Hello everyone,


    There is error message when executing my program,

    Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002.

    It is very simple, does anyone know what is wrong with the program?

    I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?

    foo.c

    Code:
    int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
    goo.c

    Code:
    extern int* p_int;
    
    int main (int argc, char** argv)
    {
            int i;
            int sum = 0;
            for (i = 0; i < 16; i++)
            {
                    sum += p_int [i]; // access violation
            }
    
            return 0;
    }

    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by George2 View Post
    I have tested that when changing from extern int* p_int to extern int p_int[16], my program is ok. But I think the two statements should be the same, right?
    In foo.c, you could even use
    Code:
    extern int p_int[];
    A pointer is not the same as an array although you can use array notation for dereferencing a pointer. (In other words, deferenced pointer notation and array notation are interchangeable in source code.)

    However...

    When the linker tries to match up symbols in the symbol table from one object file to symbols in the other, the data types must be the same, otherwise: bloooey.

    D.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Dave's answer is correct.

    > Unhandled exception at 0x00411a49 in test_entern.exe: 0xC0000005: Access violation reading location 0x00000002.
    If you want more fun, change the first element of the array and observe the change in the attempted memory access address.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM