.NET Forum / Languages / Managed C++ / August 2007
access violation in int array
|
|
Thread rating:  |
George - 03 Aug 2007 03:58 GMT 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}; [/CODE]
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; } [/CODE]
thanks in advance, George
Doug Harrison [MVP] - 03 Aug 2007 04:55 GMT >Hello everyone, > [quoted text clipped - 32 lines] >} >[/CODE] 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
 Signature Doug Harrison Visual C++ MVP
George - 03 Aug 2007 06:48 GMT 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
> >Hello everyone, > > [quoted text clipped - 50 lines] > 6. Arrays and Pointers > http://c-faq.com/aryptr/index.html Bo Persson - 03 Aug 2007 07:12 GMT :: Hi Doug, :: [quoted text clipped - 3 lines] :: 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 ::: <George@discussions.microsoft.com> wrote: [quoted text clipped - 22 lines] :::: } :::: [/CODE] George - 03 Aug 2007 08:24 GMT 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 [i] 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
> :: Hi Doug, > :: [quoted text clipped - 63 lines] > :::: } > :::: [/CODE] Bo Persson - 03 Aug 2007 09:04 GMT :: 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 [i] 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, :: [quoted text clipped - 6 lines] :: :: 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
George - 04 Aug 2007 08:50 GMT Thanks Bo Persson!
Your description is very clear. Have a good weekend!
regards, George
> :: Hi Bo Persson, > :: [quoted text clipped - 34 lines] > > Bo Persson Doug Harrison [MVP] - 03 Aug 2007 16:01 GMT >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.
 Signature Doug Harrison Visual C++ MVP
George - 04 Aug 2007 08:48 GMT Thanks Doug!
Your answer is very clear!
regards, George
> >Hi Doug, > > [quoted text clipped - 31 lines] > All of it is wrong. Read what I wrote again, and use the C FAQ link I gave > you to fill in any blanks. Ben Voigt [C++ MVP] - 03 Aug 2007 18:03 GMT > 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.
George - 04 Aug 2007 08:46 GMT Thanks for you advice, Ben!
regards, George
> > From the article, I can understand int* and int[] are not the same type. > > But [quoted text clipped - 4 lines] > element when necessary, to let you use them interchangably. But here you > are lying to the compiler, so it gets the conversion wrong.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|