when I define
int R[99];
and then later access it with
x=R[r];C[x]=7;
....
but x happens to be <0 or >99 , then the program's behavious
becomes unpredictable.
Is there a way to prevent this ?
Is there a program or debugger or compiler which
checks the array indices before executing the command and
eventually issues an error-warning when the index is out of range ? 26 1853
Sterten wrote: when I define int R[99]; and then later access it with x=R[r];C[x]=7; ... but x happens to be <0 or >99 , then the program's behavious
Surely you mean "but x happens to be <0 or >98"
becomes unpredictable.
Is there a way to prevent this ?
Yes. Don't write bad code that attempts to access beyond the bounds of
the array.
"Sterten" <st*****@aol.co m> wrote in message news:20******** *************** ****@mb-m05.aol.com... when I define int R[99]; and then later access it with x=R[r];C[x]=7; ... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable.
You must mean x < 0 or x >= 99 in the above.
You can try this:
assert((unsigne d) i < 99);
t = R[i]; Is there a way to prevent this ? Is there a program or debugger or compiler which checks the array indices before executing the command and eventually issues an error-warning when the index is out of range ?
>> int R[99]; and then later access it with x=R[r];C[x]=7;
You must mean x < 0 or x >= 99 in the above.
You can try this:
assert((unsigne d) i < 99); t = R[i];
Bare in mind that assert() is a debug tool. It helps to trap impossible
or forbidden by-design statuses. But it is not designed to trap user
level errors.
BTW, on many development systems, the assert() macro is simply ignored
at release time, due to the definition of the global NDEBUG macro.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
On Sat, 24 Jul 2004 17:26:10 -0700, "Ricardo Gibert"
<no**********@c ox.net> wrote: "Sterten" <st*****@aol.co m> wrote in message news:20******** *************** ****@mb-m05.aol.com... when I define int R[99]; and then later access it with x=R[r];C[x]=7; ... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable. You must mean x < 0 or x >= 99 in the above.
You can try this:
assert((unsigne d) i < 99);
Why the cast. If i contains a sufficiently large negative value, your
assert will be true but your next statement will invoke undefined
behavior.
t = R[i];
Is there a way to prevent this ? Is there a program or debugger or compiler which checks the array indices before executing the command and eventually issues an error-warning when the index is out of range ?
<<Remove the del for email>>
I'm thinking just at a program which traverses
the .c source program and includes
a line
if(xxx<0 || xxx>99)printf(" index out of range in line yyy")
directly before every line yyy containing
R[xxx] , which it finds.
Then the new converted .c program
can be compiles and executed when problems with array indices are suspected.
Seems not very difficult to write such a program, someone must have done it
already ?!?
Sterten wrote on 25/07/04 : I'm thinking just at a program which traverses the .c source program and includes a line if(xxx<0 || xxx>99)printf(" index out of range in line yyy")
directly before every line yyy containing R[xxx] , which it finds. Then the new converted .c program can be compiles and executed when problems with array indices are suspected.
Seems not very difficult to write such a program, someone must have done it already ?!?
Sounds to be a good idea, but it might more complicated if pointers are
involved, what happens when you 'pass an array' to a function for
example...
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
Barry Schwarz <sc******@deloz .net> writes: On Sat, 24 Jul 2004 17:26:10 -0700, "Ricardo Gibert" <no**********@c ox.net> wrote:"Sterten" <st*****@aol.co m> wrote in message news:20******* *************** *****@mb-m05.aol.com... when I define int R[99]; and then later access it with x=R[r];C[x]=7; ... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable.
You must mean x < 0 or x >= 99 in the above.
You can try this:
assert((unsigne d) i < 99);
Why the cast. If i contains a sufficiently large negative value, your assert will be true but your next statement will invoke undefined behavior.
t = R[i];
No, there is no negative value of type int that yields a value less
than 99 when converted to unsigned int. (There might be exotic
representations where this isn't the case.)
But I'd still be more comfortable with
assert(i >= 0 && i < 99);
(assuming that assert is a good way to do the check in the first
place).
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"Keith Thompson" <ks***@mib.or g> wrote in message news:ln******** ****@nuthaus.mi b.org... Barry Schwarz <sc******@deloz .net> writes: On Sat, 24 Jul 2004 17:26:10 -0700, "Ricardo Gibert" <no**********@c ox.net> wrote:"Sterten" <st*****@aol.co m> wrote in message news:20******* *************** *****@mb-m05.aol.com... > > when I define > int R[99]; > and then later access it with > x=R[r];C[x]=7; > ... > but x happens to be <0 or >99 , then the program's behavious > becomes unpredictable.
You must mean x < 0 or x >= 99 in the above.
You can try this:
assert((unsigne d) i < 99); Why the cast. If i contains a sufficiently large negative value, your assert will be true but your next statement will invoke undefined behavior.
t = R[i];
No, there is no negative value of type int that yields a value less than 99 when converted to unsigned int. (There might be exotic representations where this isn't the case.)
The idea that, "There might be some exotic implementation where this isn't the case," hadn't occurred to me. I can't imagine what it
would be like. Hmmm.
In any case, on two's complement machines, a good optimizing compiler will convert "i >= 0 && i < 99" to "(unsigned) i < 99", since
this saves several assembly instructions. The cast from int to unsigned consumes zero machine cycles on two's complement machines.
In an assert, this is not important, but anywhere else, this is a nice trick. I just thought I throw it into my post to make people
think. But I'd still be more comfortable with
assert(i >= 0 && i < 99);
For an assert() or any code that is serious about being readable for that matter, your way is much better. (assuming that assert is a good way to do the check in the first place).
-- Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.
>Sterten wrote on 25/07/04 : I'm thinking just at a program which traverses the .c source program and includes a line if(xxx<0 || xxx>99)printf(" index out of range in line yyy")
directly before every line yyy containing R[xxx] , which it finds. Then the new converted .c program can be compiles and executed when problems with array indices are suspected. Seems not very difficult to write such a program, someone must have done it already ?!?
Sounds to be a good idea, but it might more complicated if pointers are involved, what happens when you 'pass an array' to a function for example...
-- Emmanuel
I can see no principal difference, except
that the function-definition-line with
Array[] should be discarded of course.
hmm, what's with Array[function(x)] =whatever; ?
you'd have to do something like
y=function(x); if(y<Range1 or y>range2)printf ("error");
Array[y]=whatever;
but then, this can be nested to several levels...
:-(
--Guenter. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Steve |
last post by:
I'm working on an e-commerce site, and one of the things I need to do is split an existing order into two orders. The problem I'm having is not
creating the new order, but getting the remaining items from the original order cleaned up in the array. What I've tried to do so far is:
1) The data is stored in a serialized array in the order_data field in the orders table. When the order is selected, it is unserialized and called
$order_data....
|
by: Michael Drumheller |
last post by:
(If you're not interested in NumArray, please skip this message.)
I am new to NumArray and I wonder if someone can help me with
array-indexing. Here's the basic situation: Given a rank-2 array
(i.e., a matrix) it seems to be trivial, with array indexing,
to extract a subset of its *columns*. But it does not seem
to be trivial to extract a subset of its *rows*. The code
snippet below describes the problem (if it really is a problem)...
|
by: Dr John Stockton |
last post by:
Q1 :
Given an array such as might have been generated by
var A =
is there a highly effective way of reducing it to - i.e.
removing the undefineds and shifting the rest down?
A.sort().slice(0,n) // would do it, but sorts; and the number
|
by: Randell D. |
last post by:
Folks,
I can program fairly comfortably in PHP and can, for the most part using
these skills and others that I've picked up over the years manage to
read/understand most code in Javascript... so I'm just asking for a few
pointers (or the full solution if you have the time) for what I want to do.
Basically, I want to write a javascript wherby I only need to pass it the
names of form fields - then my javascript will check each form field...
|
by: Adam Hartshorne |
last post by:
As a result of a graphics based algorihtms, I have a list of indices to
a set of nodes.
I want to efficiently identify any node indices that are stored multiple
times in the array and the location of them in the array /list. Hence
the output being some list of lists, containing groups of indices of the
storage array that point to the same node index.
This is obviously a trivial problem, but if my storage list is large and
the set of...
| |
by: VK |
last post by:
A while ago I proposed to update info in the group FAQ section, but I
dropped the discussion using the approach "No matter what color the cat
is as long as it still hounts the mice". Over the last month I had
enough of extra proof that the cat doesn't hount mice anymore in more
and more situations. And the surrent sicretisme among array and hash is
the base for it.
I summarized all points in this article:...
|
by: shmartonak |
last post by:
For maximum portability what should the type of an array index be? Can
any integer type be used safely? Or should I only use an unsigned type?
Or what?
If I'm using pointers to access array elements as *(mptr+k) where I've
declared
MYTYPE *mptr;
what should be the type of 'k'? Should it be ptrdiff_t?
|
by: dennis.sam |
last post by:
Hi,
Is there away to define a multi-dimensional array with respect to the
number of dimensions the array has? For example, given a user spec of
"a b c d", I want to create a 4 dimensional array with dimensional
lengths of a, b, c and d. Thanx for any help.
|
by: memeticvirus |
last post by:
I have an array cli::array<float, 2and I would like to access a
subset of it's values by compiling an array of pointers. But, it's not
possible to create an array of type
cli:array<cli::interior_ptr<float>, 2>...
So, what do I do?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |