access violation in int array

G

Guest

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
 
D

Doug Harrison [MVP]

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

That's a classic mistake due to the mistaken notion that arrays and
pointers are the same thing, which unfortunately the linker does not catch.
You must use:

extern int p_int[16]; // or
extern int p_int[];

Either will do. The pointer declaration attempt is no good because it
essentially creates a union with the pointer object overlaying the start of
the array. To see what I mean, replace your main with:

printf("%p\n", p_int);

For more on this and other pointer/array info, see:

6. Arrays and Pointers
http://c-faq.com/aryptr/index.html
 
G

Guest

Hi Doug,


I have read your article and it is very helpful. But I have not the direct
answer to my question.


From the article, I can understand int* and int[] are not the same type. But
I think they should *function* the same. :)

In my example, I declare an int array int p_int [16] in foo.c, then making
it an int pointer by extern int* p_int in goo.c.

I think p_int in goo.c should points to the starting address of p_int (1st
element), right?

So, I think I should be able to access the elements by using p_int [index]
in goo.c.

Anything wrong in my above analysis?


regards,
George

Doug Harrison said:
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;
}

That's a classic mistake due to the mistaken notion that arrays and
pointers are the same thing, which unfortunately the linker does not catch.
You must use:

extern int p_int[16]; // or
extern int p_int[];

Either will do. The pointer declaration attempt is no good because it
essentially creates a union with the pointer object overlaying the start of
the array. To see what I mean, replace your main with:

printf("%p\n", p_int);

For more on this and other pointer/array info, see:

6. Arrays and Pointers
http://c-faq.com/aryptr/index.html
 
B

Bo Persson

George wrote:
:: Hi Doug,
::
::
:: I have read your article and it is very helpful. But I have not
:: the direct answer to my question.
::
::
:: From the article, I can understand int* and int[] are not the same
:: type. But I think they should *function* the same. :)

No, they are very, very different.

What happens when you pass an array as a function parameter is an
exception, not the rule!

::
:: In my example, I declare an int array int p_int [16] in foo.c,
:: then making it an int pointer by extern int* p_int in goo.c.

You can't make it an int pointer, when it is an array! :)

::
:: I think p_int in goo.c should points to the starting address of
:: p_int (1st element), right?

Wrong!

::
:: So, I think I should be able to access the elements by using p_int
:: [index] in goo.c.

No.

::
:: Anything wrong in my above analysis?

Just about everything! :)

When you pass an array as a parameter to a function, you cannot really
do that. You have to pass the address of the first element instead. To
be "helpful", the compiler therefore converts the name of the array
into a pointer to its first element.

This is one of the worst decisions made in early C.


Bo Persson




::: On Thu, 2 Aug 2007 19:58:03 -0700, George
:::
:::: 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;
:::: }
::::
:::
 
G

Guest

Hi Bo Persson,


I think the root cause is, compiler thinks the value of extern int* p_int is
the 1st element of the array -- i.e. the value (real value) of the 1st
element of the array is treated as the the address information, then when
using p_int in goo.c, it de-referencing the value of the 1st element as
the address?

For example, the array is,

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

p_int[0] in goo.c will make 0x00000002 as the value of pointer p_int, then
de-referencing the address of 0x00000002 -- which will cause access violation
exception?

Is my understanding correct?


regards,
George
 
B

Bo Persson

George wrote:
:: Hi Bo Persson,
::
::
:: I think the root cause is, compiler thinks the value of extern
:: int* p_int is the 1st element of the array -- i.e. the value (real
:: value) of the 1st element of the array is treated as the the
:: address information,

Yes, because your extern declaration says that it is. :)

:: then when using p_int in goo.c, it
:: de-referencing the value of the 1st element as the address?

int* p_int;

says that there is only one element, and that it is a pointer.

::
:: For example, the array is,
::
:: int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
:: 16, 17};
::
:: p_int[0] in goo.c will make 0x00000002 as the value of pointer
:: p_int, then de-referencing the address of 0x00000002 -- which will
:: cause access violation exception?
::
:: Is my understanding correct?

Yes.

An extern declaration cannot "make" something into what it is not. It
is just telling the compiler how things really are, somewhere else.

It is very important that what you tell the compiler is really true!
One way to improve on this, is to have the declaration in a .h file
and include it in both .cpp files. That way the compiler can often
tell if it is not consistent.



Bo Persson
 
D

Doug Harrison [MVP]

Hi Doug,


I have read your article and it is very helpful. But I have not the direct
answer to my question.

Actually, you were given it.
From the article, I can understand int* and int[] are not the same type. But
I think they should *function* the same. :)

They're not the same type, and they're not interchangeable, the one
exception being parameter types in function declarations.
In my example, I declare an int array int p_int [16] in foo.c, then making
it an int pointer by extern int* p_int in goo.c.

You haven't "made" it anything. What you've done is tell the compiler p_int
is something it is not, and like I said, "unfortunately the linker does not
catch" the problem.
I think p_int in goo.c should points to the starting address of p_int (1st
element), right?

No, and I told you what happens, "The pointer declaration attempt is no
good because it essentially creates a union with the pointer object
overlaying the start of the array." I went on to tell you how you could
easily observe this for yourself.
So, I think I should be able to access the elements by using p_int [index]
in goo.c.

Anything wrong in my above analysis?

All of it is wrong. Read what I wrote again, and use the C FAQ link I gave
you to fill in any blanks.
 
B

Ben Voigt [C++ MVP]

From the article, I can understand int* and int[] are not the same type.
But
I think they should *function* the same. :)

No, they don't function the same. The compiler (not the linker)
automatically converts the name of an array into a pointer to the first
element when necessary, to let you use them interchangably. But here you
are lying to the compiler, so it gets the conversion wrong.
 
G

Guest

Thanks for you advice, Ben!


regards,
George

Ben Voigt said:
From the article, I can understand int* and int[] are not the same type.
But
I think they should *function* the same. :)

No, they don't function the same. The compiler (not the linker)
automatically converts the name of an array into a pointer to the first
element when necessary, to let you use them interchangably. But here you
are lying to the compiler, so it gets the conversion wrong.
 
G

Guest

Thanks Doug!


Your answer is very clear!


regards,
George

Doug Harrison said:
Hi Doug,


I have read your article and it is very helpful. But I have not the direct
answer to my question.

Actually, you were given it.
From the article, I can understand int* and int[] are not the same type. But
I think they should *function* the same. :)

They're not the same type, and they're not interchangeable, the one
exception being parameter types in function declarations.
In my example, I declare an int array int p_int [16] in foo.c, then making
it an int pointer by extern int* p_int in goo.c.

You haven't "made" it anything. What you've done is tell the compiler p_int
is something it is not, and like I said, "unfortunately the linker does not
catch" the problem.
I think p_int in goo.c should points to the starting address of p_int (1st
element), right?

No, and I told you what happens, "The pointer declaration attempt is no
good because it essentially creates a union with the pointer object
overlaying the start of the array." I went on to tell you how you could
easily observe this for yourself.
So, I think I should be able to access the elements by using p_int [index]
in goo.c.

Anything wrong in my above analysis?

All of it is wrong. Read what I wrote again, and use the C FAQ link I gave
you to fill in any blanks.
 
G

Guest

Thanks Bo Persson!


Your description is very clear. Have a good weekend!


regards,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top