473,732 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2624
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******** *************** ***********@q77 g2000hsh.google groups.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************ ***********@fre e.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...@xmissio n.xmission.com (Kenny McCormack)
wrote:
In article <47a94b2d$0$260 24$88260...@fre e.teranews.com> ,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.

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...@xmissio n.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
6175
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 microprocessor to do a few instructions more at each array access will not make any difference what speed is concerned. Not all C applications are real-time apps.
125
6580
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 problem. http://www.jilp.org/vol9/v9paper10.pdf Specifically, they measured the overhead of a bounds
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.