Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > No limits in arrays??

Reply
Thread Tools

No limits in arrays??

 
 
Paminu
Guest
Posts: n/a
 
      10-12-2005
Is there no such a thing as an ArrayOutOfBounds exception in C?

I just did:

int main()
{
int a[4]; // a is an array that contains 4 integer.
a[5] = 999; // Thought this would give an error!
printf("a[5]: %d\n", a[5]);
return 1;
}

When I run this program

a[5]: 999

gets printed.

How is it possible to put something in a[5] when I have only specified that
a should contain 4 integers?
 
Reply With Quote
 
 
 
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      10-12-2005
No such thing in C. The results are undefined. For your compiler and
runtime environment, the program just happened to work.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-12-2005
Paminu <> writes:
> Is there no such a thing as an ArrayOutOfBounds exception in C?
>
> I just did:
>
> int main()
> {
> int a[4]; // a is an array that contains 4 integer.
> a[5] = 999; // Thought this would give an error!
> printf("a[5]: %d\n", a[5]);
> return 1;
> }
>
> When I run this program
>
> a[5]: 999
>
> gets printed.
>
> How is it possible to put something in a[5] when I have only specified that
> a should contain 4 integers?


This just came up recently.

Accessing an array element beyond the bounds of the array invokes
undefined behavior. This doesn't mean the compiler will detect the
error; it specifically means it's not required to do so. Undefined
behavior can do literally anything; the standard joke is that it can
legally make demons fly out of your nose.

In this case, you're probably just stepping on a memory location past
the end of the array. If the implementation doesn't happen to be
using ithat location for anything, it will most likely not create a
visible error (unfortunately). If it does use it for something, you
might clobber the function's return address or anything else.

The responsibility for avoiding undefined behavior is entirely yours.
Think of C as a power tool without safety features; it's effective
when used properly, but dangerous when used improperly.

Note that accessing a[4] would also invoke undefined behavior; the
valid elements are 0, 1, 2, and 3.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-12-2005
writes:
> No such thing in C. The results are undefined. For your compiler and
> runtime environment, the program just happened to work.


Here we go again.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

And *please* complain to Google about their broken interface. Search
for occurrences of the above paragraph in this newsgroup; that will
give you an idea of how big a problem it is.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      10-12-2005
In article <>,
Keith Thompson <kst-> wrote:
> writes:
>> No such thing in C. The results are undefined. For your compiler and
>> runtime environment, the program just happened to work.

>
>Here we go again.
>
> If you want to post a followup via groups.google.com, don't use
> the broken "Reply" link at the bottom of the article. Click on
> "show options" at the top of the article, then click on the
> "Reply" at the bottom of the article headers.
>
>And *please* complain to Google about their broken interface. Search
>for occurrences of the above paragraph in this newsgroup; that will
>give you an idea of how big a problem it is.


You are wasting your breath. They'll never get it.
And I'll tell you why.

Imagine that there's a mouse - and the mouse is the Usenet. You and I can
see that it is a mouse and we behave accordingly. But now there is a class
of users (we'll call them "googlers") that are wearing these funny weird
glasses that make them see not a mouse, but an elephant. Seeing an
elephant (i.e., the Usenet as a web page), they also behave accordingly.
And no amount of verbiage from us is going to convince them that it's not
an elephant - that it is only a mouse.

To make this more clear, to a googler, it doesn't make any sense to "quote"
(whatever the heck that is...), in fact, to do would be absurd, when all
the rest of the articles in the thread are right there in front of their
faces (just as clear as the trunk on that mouse, er, elephant). And no
amount of verbiage from us is going to convince them not to believe what
they see. The point is you can *never* convince someone that what they see
isn't reality. The only way you can address the problem is to help them
fix their eyesight (or help them remove their funny glasses).

>Keith Thompson (The_Other_Keith) kst- <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.


This is a cute line. Where does it come from?
Does it refer to anything beyond the obvious (The obvious being, of course,
the Iraq war) ?

 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      10-12-2005
Hi

Paminu wrote:

> Is there no such a thing as an ArrayOutOfBounds exception in C?


No. And most importantly, there are no exceptions at all in C.
But you're free to write array handling routines that somehow check for
out-of-bounds conditions and report the error... there are probably
libraries around. Something along the lines of

struct array; /* should have members size and data */
array *array_create(size_t size); /* allocate and initialize a new array */
void array_delete(array *me); /* free the array */
int array_at(array *me, size_t pos); /* return value at pos */
void array_set_at(array *me, size_t pos, int value); /* set value at pos */
....

As long as you don't need/use multi-threading, reporting errors through some
errno variable might be enough...

Maybe (although you're asking in a C newsgroup) you would be happier with
C++, as it offers what you seem to be looking for (std::vector<T>::at())?

Markus

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-12-2005
Paminu wrote:
> Is there no such a thing as an ArrayOutOfBounds exception in C?


No. Some implementations will trap some out of bounds accesses under
some circumstances, but it is not guaranteed and I'm not aware of any
that will catch all out of bounds accesses.

> I just did:


#include <stdio.h>

> int main()
> {
> int a[4]; // a is an array that contains 4 integer.
> a[5] = 999; // Thought this would give an error!
> printf("a[5]: %d\n", a[5]);


printf requires a prototype because it is a varidac function, otherwise
that is another point of undefined behaviour. The normal way to provide
the prototype is to include stdio.h

In general, ALWAYS include the headers that define prototypes for the
functions you are using, they are not provided just for the hell of it.

> return 1;
> }
>
> When I run this program
>
> a[5]: 999
>
> gets printed.
>
> How is it possible to put something in a[5] when I have only specified that
> a should contain 4 integers?


On that run there happened to be some memory at the following location.
Don't rely on this, since if you do you will find that you have
overwritten something critical and in the middle of an important demo
that your career depends on (on the result of your course) the program
will do something really annoying like displaying an insulting letter
someone has written about the person you are demonstrating the program to.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-12-2005
(Kenny McCormack) writes:
> In article <>,
> Keith Thompson <kst-> wrote:
>> writes:
>>> No such thing in C. The results are undefined. For your compiler and
>>> runtime environment, the program just happened to work.

>>
>>Here we go again.
>>
>> If you want to post a followup via groups.google.com, don't use
>> the broken "Reply" link at the bottom of the article. Click on
>> "show options" at the top of the article, then click on the
>> "Reply" at the bottom of the article headers.
>>
>>And *please* complain to Google about their broken interface. Search
>>for occurrences of the above paragraph in this newsgroup; that will
>>give you an idea of how big a problem it is.

>
> You are wasting your breath. They'll never get it.
> And I'll tell you why.

[snip]

You've already said this, and I've already explained why I disagree
(speaking of wasting breath).

[...]
>>We must do something. This is something. Therefore, we must do this.

>
> This is a cute line. Where does it come from?
> Does it refer to anything beyond the obvious (The obvious being, of course,
> the Iraq war) ?


I *think* it's originally from "Yes, Minister", a British TV show.
I had one particular thing in mind when I started using it as my
sig quote but it doesn't necessarily refer to anything specific.
And I'm *not* going to discuss politics here.

--
Keith Thompson (The_Other_Keith) kst- <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.
 
Reply With Quote
 
Anonymous 7843
Guest
Posts: n/a
 
      10-12-2005
In article <>,
Keith Thompson <kst-> wrote:
> writes:
> > No such thing in C. The results are undefined. For your compiler and
> > runtime environment, the program just happened to work.

>
> Here we go again.
>
> If you want to post a followup via groups.google.com, don't use
> the broken "Reply" link at the bottom of the article. Click on
> "show options" at the top of the article, then click on the
> "Reply" at the bottom of the article headers.
>
> And *please* complain to Google about their broken interface. Search
> for occurrences of the above paragraph in this newsgroup; that will
> give you an idea of how big a problem it is.


I briefly tried out some killfile settings to reduce my
personal noise level in this group. In trn 3.x syntax:

/googlegroups.com/HMessage-ID:j

means to kill any message whose message ID indicates that
it was posted by googlegroups.com. (Other newsreaders should
have similar capabilities.) Anyway, this cuts out quite a
bit of the homework requests, C++ questions, top-postings
and the like.

Having done that, it is immediately clear that there is
another problem: the replies[1] to each google groups post
outnumber the original by about 3:1. Though they are
well-meant (and often well-phrased) exhortations to post
correctly[2], read the FAQ, post in the correct newsgroup, do
your own homework, etc. Still, it becomes tiresome to see
the same concepts repeated. For that,

/googlegroups.com/HReferences:j

brings the noise level down *significantly*. What this does
is to scan for articles which are followups to the google
groups posts, identifying them by their reference to the
original post's message-id, and kills them. This is a bit
non-deterministic: the depth of references that a person's
newsreader preserves upon followup is not consistent. You
aren't just killing immediate followups, but possibly two,
three, or more levels down the article tree.

If the crossposts get to you more than the google groups
posts, you can try out one of these:

/Newsgroups:.*,/h:j
/Newsgroups:.*,.*,/h:j

the first kills any cross-post. The second kills only
crossposts that cross three or more newsgroups. (The
three-way cross-post killer is about the only killfile
entry I have stuck with over the years and across
all newsgroups.)

Of course, the above kill file syntax is ancient, but it
demonstrates that it is possible to quiet the google groups
noise on the receiving end. With some minor effort, it
should be possible to translate the above killfile entries
to something appropriate for your newsreader.

Of course, I'm not sure if this is the optimal approach to
take in this matter. I have two principal concerns:

* Clearly it is unfair to tar all google groups users with
the same brush, but it is also unfair that each of us
should be forced to take on the burden of tarring with a
finer brush.

* Engaging google to fix their broken interface is probably
the kindest approach, but so far no-one has gotten through
to them. Possibly, they have made their decision based on
how other newsgroups behave and are not interested in
special-casing their interface for our benefit.

Sorry, I know this is off-topic meta-discussion, but
hopefully this will make the comp.lang.c experience more
pleasant for some number of people who are as sick of
the google groups noise as I am.

_______
[1] http://www.igopogo.com/pogoplaque.jpg
[2] http://groups.google.com/support/bin...y?answer=14213
 
Reply With Quote
 
Malcolm Dew-Jones
Guest
Posts: n/a
 
      10-13-2005
Flash Gordon () wrote:
: Paminu wrote:
: > Is there no such a thing as an ArrayOutOfBounds exception in C?

: No. Some implementations will trap some out of bounds accesses under
: some circumstances, but it is not guaranteed and I'm not aware of any
: that will catch all out of bounds accesses.

You wouldn't want a compiler to catch all out of bounds errors. A common
idiom for structures is

typedef struct _something
{
struct _something * next;
char data[1];
} something ;

When used, an instance will be malloc'd with a size large enough to
contain your data and then the data member can be accessed as

a_something.data[i]

The array index may commonly appear to be "out of bounds" according to the
definition, but doesn't represent a problem.



--

This programmer available for rent.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Limits on MLPPP Vincent C Jones Cisco 7 11-12-2009 05:36 PM
EBGP Neighbor limits (for you enterprise/ISP folks) Hansang Bae Cisco 0 08-24-2004 03:11 PM
A process serving application pool 'DefaultAppPool' exceeded time limits during start up. The process id was '216'. jack ASP .Net 0 08-01-2004 09:49 PM
Cisco 1605 Access List Entry Limits AC Cisco 6 06-24-2004 09:05 PM
Aironet 1400 multipoint topology - arc limits ? indaba Cisco 0 10-29-2003 07:38 AM



Advertisments