"polas" <> wrote in message
news:ef2b1077-0cb8-4dfb-aa5d-...
> Afternoon everyone.
>
> I have a quick question about standard C. Generally speaking, in my
> experience, whenever one accesses an array there is never any bounds
> checking done (either statically during compilation or dynamically
> during runtime.) However, I was wondering if whether there is anything
> defined in the standard about this.
>
> The reason for this is I have some code conforming to ANSI C99 and
> wish to write to both arrays and a block of memory allocated by malloc
> and was wondering if I can say that there will never be any runtime
> checking done to ensure that the location I am writing to exists.
You can't be sure, because the standard doesn't say either way. It is
allowable for an implementation to do it or not do it -- or flip a coin each
time a violation happens.
In practice, most implementations don't do it, particularly on "common"
systems that most of us code for, because there is no direct hardware
support and thus it would slow things down. Some compilers have an option
that enables it, which is helpful for debugging. Certain systems, e.g. the
AS/400, always do bounds checking since it's provided by the hardware.
However, the real answer is that you should never _rely_ on bounds checking
either being present or not present. Fix your code and it won't matter.
S
--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from
http://www.teranews.com