访问动态分配的数组的界外元素/不包含SegFault [英] Accessing out-of-bounds elements of dynamically allocated arrays / w/o SegFault

查看:81
本文介绍了访问动态分配的数组的界外元素/不包含SegFault的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C开发一个程序,该程序使用链接列表(原始哈希表)数组作为数据类型来表示某些日期信息.该数组具有与一年中的月份相对应的十二个元素,并且每个月都有一个包含数据节点的链表.

I'm developing a program in C that uses an array of linked lists (a primitive hash table) as a data type to represent certain date information. The array has twelve elements corresponding to the months of the year, and each month has a linked list that contains nodes of data.

我开发了使用此数据类型的模块,并且运行良好.后来我发现我正在访问超出范围的数组元素(例如,通过索引12而不是11访问第12个元素).但是该程序始终如一地工作,没有发生任何事件.我从未收到过分段错误.此后,我已经纠正了编码错误.谁能解释为什么访问越界元素不会导致段错误?

I developed the module that used this data type and it worked fine. I later discovered that I was accessing array elements that were out of bounds (for example accessing the 12th element by the index 12 instead of 11). But the program worked consistently without incident. I never received a segmentation fault. I have since corrected the coding error. Could anybody explain why accessing out-of-bounds elements would not result in a segfault?

这不是第一次.我创建了一个动态分配的多维数组,为了进行测试,我尝试访问越界元素.该程序运行良好,产生了准确的结果,并且在大多数情况下没有出现故障.我唯一获得此成绩的时候,我不得不尝试访问实质上超出范围的元素.

This is not the first time it has happened. I have created a dynamically allocated multidimensional array, and for the sake of testing, I tried to access out-of-bounds elements. The program ran fine, produced accurate results, and did not seg fault in most circumstances. The only time I achieved one, I had to try accessing substantially out-of-bounds elements.

(这些程序当前是用于测试的Windows控制台应用程序.我正在使用MinGW进行编译.如果有帮助,我可以包括代码.)

(These programs currently are windows console applications for testing. I am compiling with MinGW. I can include code, if it would be helpful.)

推荐答案

在C语言中,访问超出其范围的数组是未定义的行为.

In C, accessing an array outside its bounds is undefined behavior.

这意味着任何事情都可能发生,包括程序的运行情况.

That means that anything can happen, including the program behaving as you might expect it to.

C语言不需要对数组访问进行边界检查,并且大多数C编译器都没有实现它.

The C language doesn't require bounds checking on array accesses, and most C compilers don't implement it.

例如,假设您声明:

int before;
int array[10];
int after;

它们在内存中的存储顺序是不确定的,但是假设它们以声明的顺序连续存储.如果尝试访问array[-1],则可以访问before.如果您尝试访问array[10],则可以访问after.

The order in which these are stored in memory is undefined, but suppose they're stored contiguously in the order they're declared. If you attempt to access array[-1], you might access before instead. If you attempt to access array[10], you might access after instead.

程序员有责任避免访问超出其范围的数组. 否则在数组之前和/或之后可能没有分配任何东西.

The burden is on the programmer to avoid accessing arrays outside their bounds. Or there might not be anything allocated before and/or after your array.

打个比方:标牌上说,只有在绿色的灯光下,我才被允许过马路.我穿过红色,却什么也没发生.为什么没有一辆汽车撞到我?" (有很多语言无法让汽车制造撞上你.C并不是其中一种.)

An analogy: "The sign says I'm only allowed to cross the street when the light is green. I crossed on red, and nothing happened. Why didn't a car hit me?" (There are languages that go out of their way to make the car hit you. C isn't one of them.)

这篇关于访问动态分配的数组的界外元素/不包含SegFault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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