int数组中的访问冲突 [英] access violation in int array

查看:66
本文介绍了int数组中的访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

执行程序时出现错误消息,


test_entern.exe中0x00411a49处的未处理异常:0xC0000005:Access

违规读取位置0x00000002。


这很简单,有谁知道程序有什么问题?


我已经测试过,当从extern int * p_int更改为extern int

p_int [16]时,我的程序没问题。但我认为两个陈述应该是

相同,对吧?


foo.c

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

展开 | 选择 | Wrap | 行号

推荐答案

2007年8月2日星期四19:58:03 -0700,乔治

< Ge * ***@discussions.microsoft.comwrote:
On Thu, 2 Aug 2007 19:58:03 -0700, George
<Ge****@discussions.microsoft.comwrote:

>大家好,

执行我的程序时出现错误信息,

test_entern.exe中0x00411a49处的未处理异常:0xC0000005:访问
违规读取位置0x00000002。

它我非常简单,有没有人知道程序有什么问题?

我测试过,从extern int * p_int更改为extern int
p_int [16],我的程序没问题。但我认为这两个陈述应该是相同的,对吧?

foo.c

>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

展开 | 选择 | Wrap | 行号


嗨Doug,

我读过你的文章,非常有帮助。但我没有直接回答我的问题。

从文章中我可以理解int *和int []不是同一类型。但是

我认为他们应该*功能*相同。 :-)


在我的例子中,我在foo.c中声明一个int数组int p_int [16],然后使用extern使得它成为一个int指针的
在goo.c中int * p_int。


我认为goo.c中的p_int应该指向p_int的起始地址(第一个

元素),对吧?


所以,我认为我应该能够在goo.c中使用p_int [index]

来访问元素。


我上面的分析有什么不对吗?

问候,

乔治


" 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.
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:

2007年8月2日星期四19:58:03 -0700,George

< Ge **** @ discuss .microsoft.comwrote:
On Thu, 2 Aug 2007 19:58:03 -0700, George
<Ge****@discussions.microsoft.comwrote:

大家好,

执行程序时出现错误消息,


test_entern.exe中0x00411a49处的未处理异常:0xC0000005:访问

违规读取位置0x00000002。


这很简单,有谁知道什么是程序有问题吗?


我测试过当从extern int * p_int更改为extern int

p_int [16]时,我的程序没问题。但我认为两个陈述应该是

相同,对吧?


foo.c

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

展开 | 选择 | Wrap | 行号


George写道:

::嗨道格,

::

::

::我看过你的文章,非常有帮助。但我没有

::直接回答我的问题。

::

::

: :从文章中我可以理解int *和int []不一样

:: type。但我认为他们应该*功能*相同。 :-)


不,它们非常非常不同。


将数组作为函数参数传递时会发生什么? br />
例外,不是规则!


::

::在我的例子中,我声明了一个int数组int p_int [16 ]在foo.c中,

::然后在goo.c中通过extern int * p_int使它成为一个int指针。


你不能做它是一个int指针,当它是一个数组! :-)


::

::我认为goo.c中的p_int应指向

的起始地址: :p_int(第一个元素),对吗?


错了!


::

::所以,我我认为我应该能够在goo.c中使用p_int

:: [index]来访问元素。


No.


::

::我上面的分析有什么不对吗?


几乎所有东西! :-)


当你将一个数组作为参数传递给一个函数时,你真的不能这样做。您必须传递第一个元素的地址。为了使
成为有用的,编译器因此将数组的名称

转换为指向其第一个元素的指针。


这是C早期作出的最糟糕的决定之一。
Bo Persson



::: 2007年8月2日星期四19 :58:03-0700,George

:::< Ge **** @ discussion.microsoft.comwrote:

:::

:::: foo.c

::::

::::
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
::::
::::
展开 | 选择 | 换行 | 行号


这篇关于int数组中的访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆