简体   繁体   English

C程序中的数组

[英]Arrays in C program

Here is my C Program Code : 这是我的C程序代码:

#include<stdio.h>

unsigned int count = 1; 

int main(void)
{
  int b = 10;
  int a[3];
  a[0] = 1;
  a[1] = 2;
  a[2] = 3; 

  printf("\n b = %d \n",b);
  a[3] = 12; 
  printf("\n b = %d \n",b); 

  return 0;
}

And the output as 和输出为

b=10 
b=12

Can anyone explain why it is and the reason behind the error. 任何人都可以解释它的原因以及错误背后的原因。

You are writing beyond the bounds of the array. 您正在写超出数组范围的内容。 This would invoke undefined behavior . 这将调用未定义的行为

You are writing at a[3] = 12; 您正在以a[3] = 12;书写a[3] = 12; to an out of bound index, which is UB and since its UB it means anything can happen so chances are you have just over-written the value at a[3] which is the location of b . 到一个越界索引UB,由于它的UB意味着任何事情都可能发生,所以您可能刚刚覆盖了a[3]的值,即b的位置。

Your program has undefined behavior . 您的程序具有未定义的行为 Depending on your result, your memory looked like this: 根据您的结果,您的记忆看起来像这样:

  a[0]   a[1]   a[2]    b   
+------+------+------+----+
|  1   |  2   |  3   | 12 |
+------+------+------+----+

b is the same as a[3] since it "sits" next to it in the memory. ba[3]相同,因为它在内存中“坐在”它的旁边。

This is only one possible scenario, your program could has a "segmentation fault" or anything else as well. 这只是一种可能的情况,您的程序可能会出现“分段错误”或其他任何情况。

It's because you're accessing a[3] , which is the 4th element, but you only defined an array of size 3. As a result, you're getting a pointer to garbage data, which happens to point to the memory location of b . 这是因为您正在访问a[3] ,这是第4个元素,但是您仅定义了一个大小为3的数组。结果,您将获得一个指向垃圾数据的指针,该指针恰好指向了内存的内存位置。 b So when you assign a[3] , you're assigning the memory location of b : 因此,当您分配a[3] ,您就是在分配b的存储位置:

#include<stdio.h>

unsigned int count = 1; 

int main(void)
{
  int b = 10;
  int a[3];
  a[0] = 1;
  a[1] = 2;
  a[2] = 3; 

  printf("\n b = %d \n",b);
  a[3] = 12; 

  printf("\n b = %d \n",b); 

 return 0;
}

The pointer memory locations point to the same thing for a[3] and b : 指针存储位置指向a[3]b

b = 10 
a[3]:0xa
b:0xa

b = 12 

Now try actually creating an array with four elements. 现在尝试实际创建一个包含四个元素的数组。 Change a[3] to a[4] , so that a[3] isn't a garbage location. a[3]更改为a[4] ,以便a[3]不是垃圾位置。 You'll see it works. 您会看到它的工作原理。

The so-called "Undefined Behaviour" can include many kinds of behaviour. 所谓的“未定义行为”可以包括多种行为。 Amongst them can be: 其中包括:

  • nothing bad happens 没什么不好的
  • you get a segmentation fault (or any other access violation) 您遇到细分错误(或任何其他访问冲突)
  • you destroy/tamper other data structures. 您破坏/篡改其他数据结构。

You have the latter here: the variable b happens to be in memory directly after the array, so it is modified due to this wrong access. 您在这里拥有后者:变量b恰好在数组之后,直接在内存中,因此由于这种错误的访问而对其进行了修改。

Maybe you can define a pointer to the address of a[3], and output it. 也许您可以定义一个指向a [3]地址的指针,然后输出它。 Then you will know the answer. 然后,您将知道答案。

because if you prints address of both variable b and a[3] like, 因为如果您同时打印变量b和a [3]的地址,

printf("%u",&b);  
printf("\n %u",&a[3]); 

then it gives same output for both variable address 然后它为两个变量地址提供相同的输出

Your program results in undefined behavior, answers here explain this "undefined behavior" trying to give some meaning to your output,which is not always true. 您的程序导致未定义的行为,此处的答案解释了此“未定义的行为”,试图使输出具有某些含义,但并不总是如此。 In-fact i try to run your program and I can see b is printed 10 in both printf and finally i get run time error (I used Visual Studio 2010) 实际上,我尝试运行您的程序,我可以看到b在两个printf中都打印了10,最后我得到运行时错误(我使用了Visual Studio 2010)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM