473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
  2.  
goo.c

Expand|Select|Wrap|Line Numbers
  1. extern int* p_int;
  2.  
  3. int main (int argc, char** argv)
  4. {
  5. int i;
  6. int sum = 0;
  7. for (i = 0; i < 16; i++)
  8. {
  9. sum += p_int [i]; // access violation
  10. }
  11.  
  12. return 0;
  13. }
  14.  

thanks in advance,
George
Aug 3 '07 #1
10 1730
On Thu, 2 Aug 2007 19:58:03 -0700, George
<Ge****@discussions.microsoft.comwrote:
>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

Expand|Select|Wrap|Line Numbers
  1. int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};

goo.c

Expand|Select|Wrap|Line Numbers
  1. extern int* p_int;
  2. int main (int argc, char** argv)
  3. {
  4.        int i;
  5.        int sum = 0;
  6.        for (i = 0; i < 16; i++)
  7.        {
  8.                sum += p_int [i]; // access violation
  9.        }
  10.        return 0;
  11. }
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

--
Doug Harrison
Visual C++ MVP
Aug 3 '07 #2
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 [MVP]" wrote:
On Thu, 2 Aug 2007 19:58:03 -0700, George
<Ge****@discussions.microsoft.comwrote:
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

Expand|Select|Wrap|Line Numbers
  1. int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};
  2.  
goo.c

Expand|Select|Wrap|Line Numbers
  1. extern int* p_int;
  2.  
  3. int main (int argc, char** argv)
  4. {
  5.         int i;
  6.         int sum = 0;
  7.         for (i = 0; i < 16; i++)
  8.         {
  9.                 sum += p_int [i]; // access violation
  10.         }
  11.  
  12.         return 0;
  13. }
  14.  

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

--
Doug Harrison
Visual C++ MVP
Aug 3 '07 #3
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
::: <Ge****@discussions.microsoft.comwrote:
:::
:::: foo.c
::::
::::
Expand|Select|Wrap|Line Numbers
  1. :::: int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  2. :::: 15, 16, 17}; 
::::
:::: goo.c
::::
::::
Expand|Select|Wrap|Line Numbers
  1. :::: extern int* p_int;
  2. ::::
  3. :::: int main (int argc, char** argv)
  4. :::: {
  5. ::::        int i;
  6. ::::        int sum = 0;
  7. ::::        for (i = 0; i < 16; i++)
  8. ::::        {
  9. ::::                sum += p_int [i]; // access violation
  10. ::::        }
  11. ::::
  12. ::::        return 0;
  13. :::: }
  14. :::: 
:::
Aug 3 '07 #4
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

"Bo Persson" wrote:
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
::: <Ge****@discussions.microsoft.comwrote:
:::
:::: foo.c
::::
::::
Expand|Select|Wrap|Line Numbers
  1. :::: int p_int [16] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  2. :::: 15, 16, 17}; 
::::
:::: goo.c
::::
::::
Expand|Select|Wrap|Line Numbers
  1. :::: extern int* p_int;
  2. ::::
  3. :::: int main (int argc, char** argv)
  4. :::: {
  5. ::::        int i;
  6. ::::        int sum = 0;
  7. ::::        for (i = 0; i < 16; i++)
  8. ::::        {
  9. ::::                sum += p_int [i]; // access violation
  10. ::::        }
  11. ::::
  12. ::::        return 0;
  13. :::: }
  14. :::: 
:::
Aug 3 '07 #5
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 [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,
::
:: 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


Aug 3 '07 #6
On Thu, 2 Aug 2007 22:48:01 -0700, George
<Ge****@discussions.microsoft.comwrote:
>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.

--
Doug Harrison
Visual C++ MVP
Aug 3 '07 #7
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.

Aug 3 '07 #8
Thanks for you advice, Ben!
regards,
George

"Ben Voigt [C++ MVP]" wrote:
>
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.
Aug 4 '07 #9
Thanks Doug!
Your answer is very clear!
regards,
George

"Doug Harrison [MVP]" wrote:
On Thu, 2 Aug 2007 22:48:01 -0700, George
<Ge****@discussions.microsoft.comwrote:
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.

--
Doug Harrison
Visual C++ MVP
Aug 4 '07 #10
Thanks Bo Persson!
Your description is very clear. Have a good weekend!
regards,
George

"Bo Persson" wrote:
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 [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,
::
:: 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


Aug 4 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: muser | last post by:
In the code I supplied before this one, the cause of the problem is an access violation error. When I run the debugger it skips into what I can only assume is the compilers version of my code. And...
0
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access...
5
by: Alex | last post by:
Hello Im working on project for my college, nevermind (but it urgent :((()... So I have this code example (in VS 6.0) in main class : REAL *Input; ....
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
1
stealwings
by: stealwings | last post by:
I have implemented a binary search and some other functions to my code, when I compile it, it gives me no error, but when I run it, it gives me an Access violation, why? Can some one explain me plz....
1
by: Beorne | last post by:
I have imported a corporate image handling COM object in my C# project. To access in a fast way the memory of the image there is a method that returns a pointer to the memory (in byte) of the...
1
by: Dameon99 | last post by:
Hi, I have just spent many hours debugging my code and now when I run it I get the following error: "An access violation (Segmentation fault) raised in your program" I have researched on this...
1
by: gnanapoongothai | last post by:
i have a program to calculate the CRC for a group of array values. SO i have program in a different file and i am passing the length and the array to the function and i am getting a access voilation...
39
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.