473,770 Members | 5,862 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
10 1769
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
4197
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
3825
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
15739
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
1890
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
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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
10053
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
10001
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,...
0
8880
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
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();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2816
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.