473,769 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is out-of-bounds portable on a multidimensiona l array?

I've checked the FAQ for this and couldn't find the answer. Is the
following code snippet portable?

int a[10][10];
a[1][4]=6;
printf("%d\n",( *a)[14]);

This prints "6" on my compiler. I've been told it's always legal, but
it seems slightly suspect to me. Can you really go out-of-bounds like
this on one of the 'sub-arrays' a[0], a[1], etc.?

(N.B. To the pedants out there: this is not meant to be a complete
program, just part of one.)

Jan 26 '06 #1
8 1833
ais523 wrote:
I've checked the FAQ for this and couldn't find the answer. Is the
following code snippet portable?

int a[10][10];
a[1][4]=6;
printf("%d\n",( *a)[14]);

This prints "6" on my compiler. I've been told it's always legal, but
it seems slightly suspect to me. Can you really go out-of-bounds like
this on one of the 'sub-arrays' a[0], a[1], etc.?
This has been discussed here before at length with strong opinions in
moth directions. I think that it is technically undefined behaviour but
will only fail on implementations that go out of their way to do bounds
checking.

I think that the following might be OK though:
int a[10][10];
a[1][4]=6;
printf("%d\n",( (int*)a)[14]);
A rough reasoning being that a must be properly aligned for an int*, so
we know the conversion will work and also obviously know it will point
to the first element of the first sub-array. Also the size of an array
is guaranteed to be the number of elements times the size of each
elements, which does not leave any room for padding to be inserted.
Finally, the array a is one object, so we are not leaving the object we
are pointing to.

Note, the last of these does not necessarily apply without the cast to
int* because a[0] is an object of type array of 10 ints.

Personally, I would be very dubious of code doing this unless there was
a very good justification for not treating it as a 2d array.
(N.B. To the pedants out there: this is not meant to be a complete
program, just part of one.)


You've obviously been reading this group since you've realised it is
full of pedants :-)
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 26 '06 #2
Flash Gordon wrote:
ais523 wrote:
I've checked the FAQ for this and couldn't find the answer. Is the
following code snippet portable?

int a[10][10];
a[1][4]=6;
printf("%d\n",( *a)[14]);

This prints "6" on my compiler. I've been told it's always legal, but
it seems slightly suspect to me. Can you really go out-of-bounds like
this on one of the 'sub-arrays' a[0], a[1], etc.?

This has been discussed here before at length with strong opinions in
moth directions. I think that it is technically undefined behaviour but
will only fail on implementations that go out of their way to do bounds
checking.

I think that the following might be OK though:
int a[10][10];
a[1][4]=6;
printf("%d\n",( (int*)a)[14]);
A rough reasoning being that a must be properly aligned for an int*, so
we know the conversion will work and also obviously know it will point
to the first element of the first sub-array. Also the size of an array
is guaranteed to be the number of elements times the size of each
elements, which does not leave any room for padding to be inserted.
Finally, the array a is one object, so we are not leaving the object we
are pointing to.

Note, the last of these does not necessarily apply without the cast to
int* because a[0] is an object of type array of 10 ints.

Personally, I would be very dubious of code doing this unless there was
a very good justification for not treating it as a 2d array.
(N.B. To the pedants out there: this is not meant to be a complete
program, just part of one.)

You've obviously been reading this group since you've realised it is
full of pedants :-)

when seeking a bi-dimensional array of char (argv) going out-of-bounds,
i had a segmentation fault long before then end of the array

/* FIRST SEEK argv[i] : OK */
for (i=0; i<argc+1000; i++)
{
printf ("argv[%d] %s\n\n", i, argv[i]);
if ((i>argc) && (*(argv+i) == NULL))
{
printf ("FIN de Tab arg %d\n\n", i);
break;
}
}

/* THIS SEEK (char)(*argv)[i] : KO long before end of last array (69/86 but it depends on the previous code...) */
/* what's more seg fault occurs long after crossing from argv to envp... */
for (i=0; i<86*100; i++)
{
printf ("%c", (char)(*argv)[i]);
}

this trouble seems not occur with array of array of int ...

Xavier

Jan 26 '06 #3
serrand wrote:

<snip>

Your post did not have anything to do with mine. It is a completely
different situation.
when seeking a bi-dimensional array of char (argv) going out-of-bounds,
i had a segmentation fault long before then end of the array
argv is not a two-dimensional array, it is an array of pointers, a
*very* different thing. Read section 6 of the FAQ, particularly
questions 6.18 and 6.19
/* FIRST SEEK argv[i] : OK */
for (i=0; i<argc+1000; i++)
{
printf ("argv[%d] %s\n\n", i, argv[i]);
As soon as you hit the last element of argv you invoke undefined
behaviour since you pass a null pointer for the %s specifier. You invoke
it again as soon as you go beyond the end of argv.
if ((i>argc) && (*(argv+i) == NULL))
{
printf ("FIN de Tab arg %d\n\n", i);
break;
}
}

/* THIS SEEK (char)(*argv)[i] : KO long before end of last array
(69/86 but it depends on the previous code...) */
/* what's more seg fault occurs long after crossing from argv to
envp... */
for (i=0; i<86*100; i++)
{
printf ("%c", (char)(*argv)[i]);
}

this trouble seems not occur with array of array of int ...


I ran across Church Road without checking if it was clear and nothing
went wrong, but when I ran across Daws Hearth Road without checking I
got hit by a 10 tonne lorry and was in hospital for a week! Why does
this only happen when running across Daws Heath Road?

Answer, blind luck.

Don't read or write off the end of an array. Every. You don't try to
read the 300th page of a 200 page book do you? Or put 200 galleons of
petrol in the petrol tank of your car which doesn't take that much? So
why do you think it is OK to do the same thing with arrays in C?
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 26 '06 #4
Flash Gordon wrote:
argv is not a two-dimensional array, it is an array of pointers, a
*very* different thing. Read section 6 of the FAQ, particularly
questions 6.18 and 6.19 thanks too much !! it's the explanation i expected ...

I ran across Church Road without checking if it was clear and nothing
went wrong, but when I ran across Daws Hearth Road without checking I
got hit by a 10 tonne lorry and was in hospital for a week! Why does
this only happen when running across Daws Heath Road?

Answer, blind luck.

Don't read or write off the end of an array. Every. You don't try to
read the 300th page of a 200 page book do you? Or put 200 galleons of
petrol in the petrol tank of your car which doesn't take that much? So
why do you think it is OK to do the same thing with arrays in C?


when reading out-of-bounds...don't we try to read the 300th page of a 200 page book ?
We can if next place is another book of the same kind ...
with bi-dimensional array, sub-arrays are contiguous... then we can pass the bounds...
the same occurs when we are certain that elements (arrays...) are contiguous...
with union don't we do that thing ?

struct TAddr {
union {
unsigned char octs[4];
unsigned long int num_ip;
} ip;
};

but i'm always following your advice (as faq 6.19) ;-)

Xavier
Jan 26 '06 #5
On 2006-01-26, serrand <xa************ @free.fr> wrote:
Flash Gordon wrote:
ais523 wrote:
I've checked the FAQ for this and couldn't find the answer. Is the
following code snippet portable?

int a[10][10];
a[1][4]=6;
printf("%d\n",( *a)[14]);

This prints "6" on my compiler. I've been told it's always legal, but
it seems slightly suspect to me. Can you really go out-of-bounds like
this on one of the 'sub-arrays' a[0], a[1], etc.?

This has been discussed here before at length with strong opinions in
moth directions. I think that it is technically undefined behaviour but
will only fail on implementations that go out of their way to do bounds
checking.

I think that the following might be OK though:
int a[10][10];
a[1][4]=6;
printf("%d\n",( (int*)a)[14]);
A rough reasoning being that a must be properly aligned for an int*, so
we know the conversion will work and also obviously know it will point
to the first element of the first sub-array. Also the size of an array
is guaranteed to be the number of elements times the size of each
elements, which does not leave any room for padding to be inserted.
Finally, the array a is one object, so we are not leaving the object we
are pointing to.

Note, the last of these does not necessarily apply without the cast to
int* because a[0] is an object of type array of 10 ints.

Personally, I would be very dubious of code doing this unless there was
a very good justification for not treating it as a 2d array.
(N.B. To the pedants out there: this is not meant to be a complete
program, just part of one.)

You've obviously been reading this group since you've realised it is
full of pedants :-)

when seeking a bi-dimensional array of char (argv) going out-of-bounds,
i had a segmentation fault long before then end of the array


argv is not a "bi-dimensional array" in the sense required for the
original question to apply.
Jan 26 '06 #6
A multi-dimensional array is really just a single dimensional array
that lets itself be treated as a multi-dimensional one - it's stored in
memory contiguously - therefore allowing you do do what you just did. A
true multi-dimensional array is an array of pointers (some would argue
with this statement, though) - the memory layout is not contiguous,
excepet for the first index of course (..the pointers), and thus, you
would not be able to do the same out-of-bounds trick.

Jan 26 '06 #7
"relient" <xl************ ***@gmail.com> writes:
A multi-dimensional array is really just a single dimensional array
that lets itself be treated as a multi-dimensional one - it's stored in
memory contiguously - therefore allowing you do do what you just did. A
true multi-dimensional array is an array of pointers (some would argue
with this statement, though) - the memory layout is not contiguous,
excepet for the first index of course (..the pointers), and thus, you
would not be able to do the same out-of-bounds trick.


Context please! Read <http://cfaj.freeshell. org/google/>.

A multi-dimensional array is really an array of arrays. The standard
says enough about array layouts that we can be sure the rows are
allocated contiguously. Given

int arr[5][5];

we can be sure that arr[0][7], *if it's legal*, is at the same
location as arr[1][2]. But there might be enough ambiguity in the
standard to allow an implementation to do bounds checking in a way
that allows arr[1][2], but forbids arr[0][7]. Or there might not;
this has been the subject of some debate, and as far as I know it
hasn't really been resolved.

An array of pointers is *not* a "true multi-dimensional array"; it's
just an array of pointers. It can be used to implement something that
acts very much like a multi-dimensional array, but it isn't one. The
standard is clear on this point.

--
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.
Jan 26 '06 #8
serrand wrote:
Flash Gordon wrote:
<snip>
I ran across Church Road without checking if it was clear and nothing
went wrong, but when I ran across Daws Hearth Road without checking I
got hit by a 10 tonne lorry and was in hospital for a week! Why does
this only happen when running across Daws Heath Road?

Answer, blind luck.

Don't read or write off the end of an array. Every. You don't try to
read the 300th page of a 200 page book do you? Or put 200 galleons of
petrol in the petrol tank of your car which doesn't take that much? So
why do you think it is OK to do the same thing with arrays in C?


when reading out-of-bounds...don't we try to read the 300th page of a
200 page book ?
We can if next place is another book of the same kind ...


In C there is no guarantee what order object will be stored.
with bi-dimensional array, sub-arrays are contiguous... then we can pass
the bounds...
That is more like going to a different chapter in the same book. See
also Keith Thompson's reply to relient.
the same occurs when we are certain that elements (arrays...) are
contiguous...
with union don't we do that thing ?
I'm really not sure what you are trying to say here.
struct TAddr {
union {
unsigned char octs[4];
unsigned long int num_ip;
} ip;
};

but i'm always following your advice (as faq 6.19) ;-)


Good.

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 26 '06 #9

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

Similar topics

4
8636
by: FilexBB | last post by:
Hi Folks, I have tried to redirect system.out for a while and then set it back, but it can't set it back as following program snapshot ByteArrayOutputStream baos = new ByteArrayOutputStream(); System.setOut(new PrintStream(baos, true)); System.out.println("hello"); System.err.println(baos); System.setOut(System.out);
4
36753
by: Merlin | last post by:
Hi there, I would like to check if a string is a valid zip code via Javascript. Length and existents are already checked. How can I find out if the string contains characters other than numbers? Example: 834F7 schould be false since it countains an F character
5
8387
by: Mike Carroll | last post by:
I have a COM server that's generally working ok. But one of its methods, when the IDL gets read by the Intertop layer, has parameters of type "out object". The C# compiler tells me that it can't convert from object to out object, or from int* to out object, or from object to out object. In fact, I can't find anything at all that it will accept as a parameter on the call. I don't think this is a COM issue, because if I wrote my own C#...
4
13791
by: Steve B. | last post by:
Hello I'm wondering what is exactly the difference between "ref" and "out" keywords. Thanks, Steve
2
2331
by: Chua Wen Ching | last post by:
Hi there, I am wondering the difference between attribute and out keywords. Are they the same or does it serve any different purposes? I saw the and out usage in this code, and i had idea, does it had differences. internal static extern Boolean ReadFile(IntPtr hFile, Byte lpBuffer,
4
2372
by: Jon | last post by:
Why are out parmeters included in an BeginInvoke? They seem to do nothing? TestProgam: using System; namespace TempConsole { class App { public delegate void MyDelegate( out byte b, out string s );
14
7354
by: stic | last post by:
Hi, I'm in a middle of writing something like 'exception handler wraper' for a set of different methodes. The case is that I have ca. 40 methods form web servicem, with different return values (and types), and with out parmeters. What I want to do is to support each method call with exception (http 404, soap exception, and other types of exceptions) and wrap it with try & catch (a lots of catch ;-)
6
5066
by: nick | last post by:
For example: public static void FillRow(Object obj, out SqlDateTime timeWritten, out SqlChars message, out SqlChars category, out long instanceId)
6
1501
by: selva | last post by:
hi, currently iam working on c# i need to pass a parameter inside a function which is a OUT parameter and i need to pass it as a reference how to do this? please help me regarding this issue........ regards bharathi
6
14386
by: carlos123 | last post by:
Ok guys, check this out! Im getting an error "Error: Index: 0, Size: 0" not sure why. try{ // Create file FileWriter fstream = new FileWriter("database.txt"); BufferedWriter out = new BufferedWriter(fstream); for( int i=0; i<firsta.size(); i++) {
0
10215
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
10049
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...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
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
7410
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
6674
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
5307
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
3964
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
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.