C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

negative array index?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
s5n
Guest





PostPosted: Mon Feb 19, 2007 10:40 pm    Post subject: negative array index? Reply with quote



Hello, all!

i've seen the following construct in several pieces of C code, and i
can't for the life of me figure out (A) what it is and (B) why it is
legal:

.... myarray[-1] ...

Can someone please explain to me what it means to have a negative
array index?

Example snippets, taken from the SpiderMonkey source code:

stephan@ludo:~/cvs/SpiderApe/src/js> grep -e '\[-1' *.*

js.c: value[-1] = '=';
....
jsstr.c: argv[-1] = STRING_TO_JSVAL(str);


If someone could explain this phenomenon to me i could certainly sleep
better at night.

:)
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Douglas A. Gwyn
Guest





PostPosted: Thu Feb 22, 2007 10:11 am    Post subject: Re: negative array index? Reply with quote



"s5n" <stephan (AT) s11n (DOT) net> wrote in message
news:clcm-20070219-0003 (AT) plethora (DOT) net...
Quote:
i've seen the following construct in several pieces of C code, and i
can't for the life of me figure out (A) what it is and (B) why it is
legal:
... myarray[-1] ...

Your C textbook should have explained that pointer arithmetic is
actually the more fundamental notion, and that a[i] is treated as
equivalent to *(a+i). [Also recall that in almost every context
(operand of sizeof being the notable exception), the name of an
array is automatically converted to a pointer to the first element
of that array, not relevant in this instance, but it explains why
the usual subscripting works for a declared array.] Now,
myarray[-1] wouldn't be valid if "myarray" were actually the
name of a declared array, but code using such a construct will
actually be using a pointer variable, not an array name. p[-1]
merely means *(p-1), i.e., the contents of the object located
immediately below where the pointer currently points.

The following diagram may help; there is a char array (whose
name, if any, we don't care about) containing the sequence of
characters spelling "cherrybomb", and p is a pointer to type
char that has previously been set up to point to the first 'b';
then p[-1] designates the contents of the preceding char,
currently 'y'.

c h e r r y b o m b
... . . . . . ^ . . .
... . . . . . p . . .

[You need to view that using a constant-width font for it to
line up properly.]
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Hans-Bernhard Bröker
Guest





PostPosted: Thu Feb 22, 2007 10:12 am    Post subject: Re: negative array index? Reply with quote



s5n wrote:
Quote:
i've seen the following construct in several pieces of C code, and i
can't for the life of me figure out (A) what it is and (B) why it is
legal:

... myarray[-1] ...

That would be because, ripped out of context like this, it's (A)
meaningless, and (B) there's no reason to assume it might be legal.

Quote:
Can someone please explain to me what it means to have a negative
array index?

The clue, if any, would be that the above doesn't necessarily mean that
there's an actual array index (-1) involved.

As long as myarray is the wrong name for this thing, i.e. it is *not*
actually an array, the above can work. E.g. if myarray is actually a
pointer to the 5th element of an actual array, the above expression is
just a reference to its 4th element.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Markus Moll
Guest





PostPosted: Thu Feb 22, 2007 10:12 am    Post subject: Re: negative array index? Reply with quote

Hi

s5n wrote:
Quote:
i've seen the following construct in several pieces of C code, and i
can't for the life of me figure out (A) what it is and (B) why it is
legal:

... myarray[-1] ...

Can someone please explain to me what it means to have a negative
array index?

Well, a[b] means *(a+b), so myarray[-1] means *(myarray-1).

Quote:
If someone could explain this phenomenon to me i could certainly sleep
better at night.

Well, if myarray does not point to the beginning of an array, but inside
some array, then myarray[-1] just accesses the element one before myarray.

Markus
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Andrei Voropaev
Guest





PostPosted: Thu Feb 22, 2007 10:12 am    Post subject: Re: negative array index? Reply with quote

On 2007-02-19, s5n <stephan (AT) s11n (DOT) net> wrote:
Quote:
i've seen the following construct in several pieces of C code, and i
can't for the life of me figure out (A) what it is and (B) why it is
legal:

... myarray[-1] ...

Read K&R "The C Programming Language".

"The expression E1[E2] is identical (by definition) to *((E1)+(E2))"

--
Minds, like parachutes, function best when open
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Clark S. Cox III
Guest





PostPosted: Thu Feb 22, 2007 10:12 am    Post subject: Re: negative array index? Reply with quote

s5n wrote:
Quote:
Hello, all!

i've seen the following construct in several pieces of C code, and i
can't for the life of me figure out (A) what it is and (B) why it is
legal:

.... myarray[-1] ...

Can someone please explain to me what it means to have a negative
array index?

It means the same as a positive index: a[b] is equivalent to *(a + b),
so a[-1] is equivalent to *(a - 1)

If you have a pointer that points into an array (i.e. not at the first
element), a negative index is perfectly legal:

int arr[10] = ...;
int p = arr +5;

p[-5] == arr[0];
p[-4] == arr[1];
p[-3] == arr[2];
p[-2] == arr[3];
p[-1] == arr[4];
p[0] == arr[5];
p[1] == arr[6];
p[2] == arr[7];
p[3] == arr[8];
p[4] == arr[9];

--
Clark S. Cox III
clarkcox3 (AT) gmail (DOT) com
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Kenneth Brody
Guest





PostPosted: Thu Feb 22, 2007 10:12 am    Post subject: Re: negative array index? Reply with quote

s5n wrote:
Quote:

Hello, all!

i've seen the following construct in several pieces of C code, and i
can't for the life of me figure out (A) what it is and (B) why it is
legal:

... myarray[-1] ...

Can someone please explain to me what it means to have a negative
array index?

Example snippets, taken from the SpiderMonkey source code:

stephan@ludo:~/cvs/SpiderApe/src/js> grep -e '\[-1' *.*

js.c: value[-1] = '=';
...
jsstr.c: argv[-1] = STRING_TO_JSVAL(str);

If someone could explain this phenomenon to me i could certainly sleep
better at night.

You can't (legally) take a negative subscript of an array.

However, it may be that "value" and "argv" in the above examples
aren't really arrays, but pointers. And, if the code guarantees
that they point into an array at something other than array[0],
they are valid. In that case, you can think of the above as:

*(value-1) = '=';
or
*(argv-1) = STRING_TO_JSVAL(str);

That is, "the previous array entry is assigned the value of...".

Perhaps these references are in a loop, in which the pointer has
been incremented prior to the lines with the "[-1]" reference?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap (AT) gmail (DOT) com>
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Jack Klein
Guest





PostPosted: Thu Feb 22, 2007 10:12 am    Post subject: Re: negative array index? Reply with quote

On 19 Feb 2007 16:40:37 GMT, "s5n" <stephan (AT) s11n (DOT) net> wrote in
comp.lang.c.moderated:

Quote:
Hello, all!

i've seen the following construct in several pieces of C code, and i
can't for the life of me figure out (A) what it is and (B) why it is
legal:

... myarray[-1] ...

Can someone please explain to me what it means to have a negative
array index?

Example snippets, taken from the SpiderMonkey source code:

stephan@ludo:~/cvs/SpiderApe/src/js> grep -e '\[-1' *.*

js.c: value[-1] = '=';
...
jsstr.c: argv[-1] = STRING_TO_JSVAL(str);


If someone could explain this phenomenon to me i could certainly sleep
better at night.

:)

The code might be valid, and it might not be. Can't say for sure
without seeing where "value", "myarray", and "argv" come from.

This is perfectly legal C (if included inside a function, of course):

char hello [] = "hello";
char *cp = hello + 4;
cp [-4] = 'j';

The result is that the character array 'hello' will contain the string
"jello"

So if you have a pointer into an array object, beyond the first
element, you can use a negative subscript so long as it does not try
to access memory beyond the beginning of the array.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
s5n
Guest





PostPosted: Sun Feb 25, 2007 10:11 am    Post subject: Re: negative array index? Reply with quote

On Feb 19, 5:40 pm, "s5n" <step...@s11n.net> wrote:
Quote:
Can someone please explain to me what it means to have a negative
array index?

A general response to all who have answered my question:

Thanks very much for the explanation. More digging around in the
SpiderMonkey sources reveals that they do indeed over-allocate an
array and then tell client code that the array starts somewhere else,
keeping a few elements for themselves for bookkeeping.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.