When Range checking is turned off and some code is reading, (not writing) outside an array bounds can this produce access violations?


I got a simple question, if range checking is turned off in the compiler options and some code in a program is reading (not writing) beyond an array's  or string bounds, for example array[index] where index is one higher than the length of the array, can only that read operation produce access violations ?

I think it can but i'm not 100% certain. For example when the end of the allocated array's memory is located just at the edge / border of our programs allocted memory and just near it sits memory from another application. Or wouldn't this in general produce an access violation.

The reason i'm asking is that range checking had been turned for years in our code and i just turned it on in a debug version for me to test with to find out that there are a few places were our code reads 1 element beyond the arrays index. If range checking is turned on you'll get  a range check exception here but if it's turned off it seems to work without a problem. I know writing there will produce memory corruption but i'm just wondering if this can produce access violations at some point if the memory is just being read. I'd like to know since we are in the process of tracing an annoying access violation that happens with clients, which we can't reproduce and has no stack trace and the memory address of the EIP and accessed memory seems to lie beyond our programs memory limits.I wonder if we should focus on the those kind of range check errors or not to trace down that access violation, since the range checking system only mentions them when we actually go out of range. So there might be more code that does it which we haven't seen yet. (Just to be clear, the places where it did happen i already changed the code to prevent it from happening, but there might be other places where it happens)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

With range checking enabled you always get an ERangeError, when array indexes are out of bounds, otherwise the out-of-bounds memory will be accessed and could get corrupted.

It is recommended enabling range checking, to ensure that array indexes are always within the array bounds.

This is from the book "Delphi in a Nutshell" by Ray Lischner...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Compile below test app both, with and without range checking, and check program behavior...

unit Unit1;


  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    { Private-Deklarationen }
    { Public-Deklarationen }

  Form1: TForm1;


  t: array of string;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

procedure TForm1.Button2Click(Sender: TObject);
  ShowMessage('array size = '+inttostr(Length(t)));

procedure TForm1.FormCreate(Sender: TObject);

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);


Open in new window

> When Range checking is turned off and some code is reading, (not writing) outside an array bounds can this produce access violations?

Straight answer, no, you will not get AV when reading (but too risky, there is no guarantee for that). But definitely you get AV or freeze when writing.
It differs from one Delphi version to another on what to expect as a result of read/write outside the arrays declared range.
In Delphi 7 When trying to write beyond the upper bound of the array you get AV, but in Delphi XE the application freezes without error (Alt+Ctr+Del to terminate). In both versions, writing under the lower bound you get no error.
But generally, avoid accessing memory (read/write) that is out of range, risk is there to get AV/freeze or worst corrupt the memory.

You may read is article from eureka about AV.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.