473,324 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Bounds checking

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.

Cheers,
Nick
Feb 5 '08 #1
7 2596
polas said:
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.
A bounds violation invokes undefined behaviour; the Standard has nothing to
say about what will happen when a bounds violation occurs. Therefore, an
implementation can respond to a bounds violation in any way it likes - it
can ignore it, crash, report it, whatever. And, as long as bounds checking
doesn't break a strictly conforming program, the "as if" rule cuts in -
implementations can do whatever they like in the background as long as the
computational result of a strictly conforming program is not changed by
their behaviour.

In other words, the Standard neither forbids nor requires bounds checking.
A conforming implementation could certainly do bounds checking. Many do
not, because of the overhead it imposes on every program. Correct programs
don't need bounds checking. On the other hand, bounds checking can be very
useful during development. For this reason, an implementation that has
optional bounds checking (on during dev and test, off for the production
code) will score highly with its customers, on that issue at least.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Feb 5 '08 #2
polas <ni**@helpforce.comwrote:
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.
If a compiler supports this sort of checking, it's probably disabled by
default. I only know of one compiler*, actually, which supports this--TinyCC.
With TinyCC you have to enable it, using the -b switch.

Otherwise, the behavior is undefined as mentioned elsethread, and is usually
also unspecified by the compiler, so anything can happen (as opposed to
TinyCC w/ -b, where it specifies what it does).

* That is, in the form typically distributed, and without patching.

Feb 5 '08 #3
polas wrote:
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.
Bounds checking is neither required nor disallowed by the Standard. As
far as specific implementations are concerned for gcc the
options '-fmudflap', '-fmudflapth' and '-fmudflapir' enable and
configure some amount of bounds checking. A separate
library, 'libmudflap' needs to be linked with your program. For MSVC
you can use the '/RTC' and '/GS' options.

In addition you can use third-party tools like Purify or Valgrind to
test for memory access errors.

<http://valgrind.org/>
<http://www-306.ibm.com/software/awdtools/purifyplus/>

Feb 5 '08 #4
"polas" <ni**@helpforce.comwrote in message
news:ef**********************************@q77g2000 hsh.googlegroups.com...
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

Feb 6 '08 #5
In article <47***********************@free.teranews.com>,
Stephen Sprunk <st*****@sprunk.orgwrote:
....
>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.
In much the same way as you should never wear seat belts.

Drive perfectly safely and it won't matter.

Feb 6 '08 #6
On 6 Feb, 13:04, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
In article <47a94b2d$0$26024$88260...@free.teranews.com>,Step hen Sprunk <step...@sprunk.orgwrote:

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

In much the same way as you should never wear seat belts.

Drive perfectly safely and it won't matter.
Thanks for all the replies and help - that clears it up for me. The
actual reason I was asking was with respect to efficiency, as
mentioned previously, bounds checking can be expensive and languages
which always do it have this overhead.

Nick
Feb 7 '08 #7
polas <n...@helpforce.comwrote:
gaze...@xmission.xmission.com (Kenny McCormack) wrote:
Stephen Sprunk <step...@sprunk.orgwrote:
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.
In much the same way as you should never wear seat
belts.
No, in the same way as you should never _rely_ on
seatbelts. Try reading what people say, as opposed to
what you think they say.
Drive perfectly safely and it won't matter.
Driving safely is always good advice, irrespective of
whether a there are seat belts. Note that many busses
do not have seatbelts. That doesn't mean or suggest
that drivers can afford to be reckless.
Thanks for all the replies and help - that clears it
up for me. The actual reason I was asking was with
respect to efficiency, as mentioned previously, bounds
checking can be expensive and languages which always
do it have this overhead.
Yes, but not as much as you might think.

Note that C's pointer freedom comes at a cost in that
certain optimisations can't be performed.

--
Peter
Feb 7 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: jacob navia | last post by:
As everybody knows, the C language lacks a way of specifying bounds checked arrays. This situation is intolerable for people that know that errors are easy to do, and putting today's powerful...
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.