473,761 Members | 4,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1768
On Thu, 2 Aug 2007 19:58:03 -0700, George
<Ge****@discuss ions.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****@discuss ions.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****@discuss ions.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****@discuss ions.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****@discuss ions.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, "unfortunat ely 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****@discuss ions.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, "unfortunat ely 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

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

Similar topics

11
4194
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 I don't know where the problem lies in the code. Can anyone run it through there own compiler please and tell me where and what the problem is. #include <iostream> #include <iomanip> #include <fstream>
0
2724
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 violations aren't part of the standard C++ >> exception handling support. On Windows, a particular MSVC compiler >> option enables Microsoft's Structured Exception Handling (SEH) in C++ >> EH so that a catch (...) will catch an access violation. ...
5
2354
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
3820
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
1537
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. Thanks in advance. // SpanishBreakPredict.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; #include <fstream> using std::ifstream; using std::ofstream;
1
15736
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 underlying image. This method can be used to read as well to rewrite the pixel values. In the interface .idl file the method is declared as: HRESULT GetPlane( BYTE ** pPlane, long *dim);
1
2641
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 and found that it is usually the result of faulty pointers or memory that is not allocated. However I have allocated all of my memory corectly (I feel) and my pointers look to be correct.. Is there another reason my code could be causing this error?...
1
1887
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 error. couldnt understand why? plz help, here is my code crc file code #include<stdio.h> #include<windows.h> #define WIDTH 16 #define TOPBIT (1<<15)
39
4291
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 just now, I am getting the subject error message (specifically: PHP has encountered an access violation at 00F76E21). The error is NOT occurring on every page request (but it is on most of them) and, when I get the error, simply pressing <F5to...
0
9538
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9975
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9909
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7342
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.