473,387 Members | 1,493 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,387 software developers and data experts.

out of range array subscript

The n869 draft says:

J.2 Undefined behavior

[#1] The behavior is undefined in the following
circumstances:

-- An array subscript is out of range, even if an object
is apparently accessible with the given subscript (as
in the lvalue expression a[1][7] given the declaration
int a[4][5]) (6.5.6).

I am wondering if a cast can change this behaviour to something well
defined, ie if the las following line is ok:

int a[4][5];

a[1][7] = 3; /* <- undefined behaviour */

((int*)a[1])[7] = 42; /* defined or not? */

--
Richard
Nov 14 '05 #1
5 3411
Richard Delorme wrote:
The n869 draft says:

J.2 Undefined behavior

[#1] The behavior is undefined in the following
circumstances:

-- An array subscript is out of range, even if an object
is apparently accessible with the given subscript (as
in the lvalue expression a[1][7] given the declaration
int a[4][5]) (6.5.6).

I am wondering if a cast can change this behaviour to something well
defined, ie if the las following line is ok:

int a[4][5];

a[1][7] = 3; /* <- undefined behaviour */

((int*)a[1])[7] = 42; /* defined or not? */


Still undefined. The cast does not change the type of the object
itself, only the type of the expression you are using to access the
object.

For example, imagine an array bounds checking implementation which
represents a[1] as the triple (pointer to a[1][], offset 0, size 5).
The cast may preserve this representation, just as it would with a
pointer to a malloced area. The lvalue (a[1][], offset 0, size 5)[7]
translates into *(a[1][], offset 7, size 5), and fails on the array
bounds check that offset < size. (Or the check offset <= size, if you
had not dereferenced the pointer.)

--
Hallvard
Nov 14 '05 #2
On 3 May 2004, Hallvard B Furuseth wrote:
Richard Delorme wrote:
The n869 draft says:

J.2 Undefined behavior

[#1] The behavior is undefined in the following
circumstances:

-- An array subscript is out of range, even if an object
is apparently accessible with the given subscript (as
in the lvalue expression a[1][7] given the declaration
int a[4][5]) (6.5.6).

I am wondering if a cast can change this behaviour to something well
defined, ie if the las following line is ok:

int a[4][5];

a[1][7] = 3; /* <- undefined behaviour */

((int*)a[1])[7] = 42; /* defined or not? */


Still undefined. The cast does not change the type of the object
itself, only the type of the expression you are using to access the
object.


So let's alter it slightly just to make things unclear:

((int*)(a+1))[7] = 42; /* still UB? */

Nov 14 '05 #3
Jarno A Wuolijoki wrote:
On 3 May 2004, Hallvard B Furuseth wrote:
int a[4][5]; ((int*)a[1])[7] = 42; /* defined or not? */


Still undefined. The cast does not change the type of the object
itself, only the type of the expression you are using to access the
object.


So let's alter it slightly just to make things unclear:

((int*)(a+1))[7] = 42; /* still UB? */


Sure, still UB. `a+1' degenerates to `&a[0] + 1' = `&a[1]', so it still
refers to an object where the [7] index goes above the array bound.

OTOH, this is OK:

((int*)&a)[1*5 + 7] = 42;

because `&a' gives the address of the entire object `a', which consists
of 20 `int's, and 1*5 + 7 < 20.

--
Hallvard
Nov 14 '05 #4
On 3 May 2004, Hallvard B Furuseth wrote:
Jarno A Wuolijoki wrote:
On 3 May 2004, Hallvard B Furuseth wrote:
int a[4][5];
((int*)a[1])[7] = 42; /* defined or not? */

Still undefined. The cast does not change the type of the object
itself, only the type of the expression you are using to access the
object.


So let's alter it slightly just to make things unclear:
((int*)(a+1))[7] = 42; /* still UB? */


Sure, still UB. `a+1' degenerates to `&a[0] + 1' = `&a[1]', so it still
refers to an object where the [7] index goes above the array bound.


Wouldn't, say, a+1+2 be one as well by that logic?

a+1 becomes &a[1] and &a[1]+2 would be an out of bounds access of a[1].

Or does (int*)&a[1] somehow descend to the "subobject" in a way
mere &a[1] doesn't?

Nov 14 '05 #5
[I *think* I have all the attributions correct...]

Someone wrote:
> int a[4][5];
> ((int*)a[1])[7] = 42; /* defined or not? */ On 3 May 2004, Hallvard B Furuseth wrote:
Still undefined. The cast does not change the type of the object
itself, only the type of the expression you are using to access the
object.
Jarno A Wuolijoki wrote:
So let's alter it slightly just to make things unclear:
((int*)(a+1))[7] = 42; /* still UB? */
On 3 May 2004, Hallvard B Furuseth wrote:
Sure, still UB. `a+1' degenerates to `&a[0] + 1' = `&a[1]', so it still
refers to an object where the [7] index goes above the array bound.

In article
<news:Pi*************************************@melk inpaasi.cs.Helsinki.FI>
Jarno A Wuolijoki <jw******@cs.Helsinki.FI> writes:Wouldn't, say, a+1+2 be one as well by that logic?
Assuming "be one" means "be an instance of undefined behavior": no.
Let me give the intermediate expressions names here:
a+1 becomes &a[1] and &a[1]+2 would be an out of bounds access of a[1].
a+1 and &a[1] denote the same thing, a value of type "pointer to
array 5 of int" pointing to the row of 5 "int"s in a[1]:

int (*p)[5] = &a[1];
/*
* Now we have, e.g.:
*
* a[0]: { 0, 1, 2, 3, 4}
* a[1]: { 5, 6, 7, 8, 9}
* a[2]: {10,11,12,13,14}
* a[3]: {15,16,17,18,19}
*
* and p points to all of a[1].
*/

Adding 2 to this value steps forward by two of the objects to
which this points:

int (*q)[5] = p + 2;

Since "p" points to one complete row of 5 "int"s, q steps forward
by two complete rows of 5 "int"s, and now points to all of a[3].

Note that *p is an array -- it names all 5 elements of a[1] --
and *q is also an array (all 5 elements of a[3]). Because *p
and *q are arrays, they are subject to The Rule about arrays
and pointers in C, namely:

In a value context, an object of type "array N of T"
becomes a value of type "pointer to T", pointing to the
first element of that array, i.e., the one with subscript 0.

So if we write (*p)[2], that puts *p -- an array object --
into a value context to subscript it with "[2]", and thus converts
from "the entire row {5,6,7,8,9} as an object" into "a pointer
to the array's first element, i.e., a pointer to the int 5".
Subscripting by 2 is really pointer arithmetic, where to add
2 we move forward by 2 of whatever it is that this new pointer
points to -- in this case, 2 "int"s. This takes us from pointing
to the 5 to pointing to the 7, and then the last step is to
indirect again, giving the "int" 7 as an object.

Hence:

(*p)[2] += 70;

makes a[1][2] change from 7 to 77. We can write (*p) as p[0]
of course:

p[0][2] += 70; /* same thing */

and moreover, we can use pointer arithmetic on p -- as we did
to get q -- as part of the subscripting operation:

p[1][2] -= 20;

Since p[1] "means" *(p + 1), we move forward by one of the things
that "p" points to, i.e., one "array 5 of int" row in the array
named "a". Now we point to the entire row {10,11,12,13,14}. The
indirection gets us the entire array object, which "decays" to a
pointer per The Rule for the [2] step. Subscriping this pointer
works just as before, and p[1][2] names the same single "int" as
a[2][2], which in the example above is "12" initially. Subtracting
20 from it changes a[2][2] from 12 to -8.
Or does (int*)&a[1] somehow descend to the "subobject" in a way
mere &a[1] doesn't?


Here we have &a[1] -- a pointer to the entire row {5,6,7,8,9} in
the example above -- and convert the pointer through pointer casting
into some other pointer. What exactly does this do? The rules
here are at least to some extent up to the implementation. The
"input" type (before the cast) is "int (*)[5]" or "pointer to array
5 of int". The "output" type is "int *", or "pointer to int".
The implementor gets to decide how to achieve the conversion and
what the result should be.

Suppose a hypothetical Evil Implementor decides that the rule is
"conversion of `pointer to array N of T' to `pointer to T' finds
the N-1th element of the array and gives you that pointer". (This
would be quite *surprising* but I do not believe the C standards
forbid it. It might even be the "natural" implementation on certain
ancient IBM mainframes, the ones where Fortran array subscripting
worked from the top down, as it were.) In this case, the result
of (int *)p -- where p points to the entire row {5,6,7,8,9} --
would be a pointer to the element a[1][4], currently holding 9.

Of course, the "usual" implementation is to take the *lowest*
machine byte or word address, so that (int *)p is just a pointer
to the element a[1][0], currently holding 5.

This pointer cast, like all pointer casts, should be viewed with
suspicion. ("Cast a jaundiced eye upon the pointer"? :-) See
<http://www.phrases.org.uk/bulletin_board/19/messages/133.html>)
C's history, particularly with "const", makes some pointer
casts inevitable in some code, but the more you "evit" :-) them
the better, in general. (Aside: www.m-w.com claims "evitable"
*is* a word. I thought it was a "lost positive" myself.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6

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

Similar topics

4
by: Tom Page | last post by:
Hello all I have a question that may or may not be simple - I need my three dimensional array to be of the form: array where jj and kk range from 0 to some constant fixed at the very...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
3
by: uche | last post by:
Please give me some feed back on this issue: Here is the complier error: hexdmp.cpp: In function `void output(unsigned char, int, bool&)': hexdmp.cpp:133: error: invalid types `unsigned char'...
3
by: jock1up | last post by:
I have this array and Dim statement: Dim curSales(1 to 5) as Currency Using an If clause, I was wondering which one might verify that the array subscript called intX was valid? I realize...
13
by: Ivan | last post by:
Hi, What is the best syntax to use a char to index into an array. /////////////////////////////////// For example int data; data = 1;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.