[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

offset 1 list indexing

Michael Press

6/15/2011 4:02:00 AM

I want to index into a list using offset one indexing.
The obvious thing to do is make a pointer that points
one element before the list.

void work_on_stuff(int *offset0, int length)
{
int offset1 = offset0 - 1;

for(idx = 1; idx <= length; idx++)
{
... offset1[idx] ...
}
}

Is this likely to fail? Possible to fail?
Very unlikely to fail? Not guaranteed by the standard?

--
Michael Press
56 Answers

Malcolm McLean

6/15/2011 4:30:00 AM

0

On Jun 15, 7:02 am, Michael Press <rub...@pacbell.net> wrote:
> I want to index into a list using offset one indexing.
> The obvious thing to do is make a pointer that points
> one element before the list.
>
> void work_on_stuff(int *offset0, int length)
> {
>     int offset1 = offset0 - 1;
>
>     for(idx = 1; idx <= length; idx++)
>     {
>          ... offset1[idx] ...
>     }
>
> }
>
> Is this likely to fail? Possible to fail?
> Very unlikely to fail? Not guaranteed by the standard?
>
Moderately unlikely to fail. (I presume offset1 is an int * and this
is just a typo).

offset0 is a pointer to a valid chunk of memory. offset1 points to an
invalid chunk of memory before it. It may be occupied by another
variable, or it might be used for something else. The most likely
problem is that you have segmented memory and offset0 happens to be at
the start of a segment. offset1 might then turn into a wild pointer.
However a really sophisticated compiler with bounds checking might
trap offset1 as an illegal pointer. The standard makes no guarantees
that even storing offset0 - 1 will do what you intend. You're unlikely
to be running on either type of system, however.

John Doe

6/15/2011 4:59:00 AM

0

On Tue, 14 Jun 2011 21:02:25 -0700, Michael Press wrote:

> I want to index into a list using offset one indexing.
> The obvious thing to do is make a pointer that points
> one element before the list.

> Is this likely to fail?

No.

> Possible to fail?

Yes.

> Very unlikely to fail?

Depends upon the platform. On a typical flat-memory architecture, it's
unlikely to fail, although I suppose that it could fail if the function is
inlined and the compiler is being especially clever.

> Not guaranteed by the standard?

AFAICT, it's okay by the standard so long as offset0 actually points at
least one element into an array. If it points to the first element of an
array, the behaviour is undefined.

Whether or not it works practice comes down to whether the architecture
cares about referencing invalid memory locations rather than actually
accessing them, e.g. segmented memory, trap representations, etc.

Tim Rentsch

6/15/2011 9:52:00 AM

0

Michael Press <rubrum@pacbell.net> writes:

> I want to index into a list using offset one indexing.
> The obvious thing to do is make a pointer that points
> one element before the list.
>
> void work_on_stuff(int *offset0, int length)
> {
> int offset1 = offset0 - 1;
>
> for(idx = 1; idx <= length; idx++)
> {
> ... offset1[idx] ...
> }
> }
>
> Is this likely to fail? Possible to fail?
> Very unlikely to fail? Not guaranteed by the standard?

(Assuming 'offset0' actually points to the start of an array
rather than somewhere further on:)

Not guaranteed by the Standard, and a bad idea even though
currently it probably works in many (most?) compilers.

As compilers get more aggressive at code improvement, code
like this with undefined behavior gets more likely to cross
over into the compiler equivalent of the Neutral Zone. If
and when that happens, most likely one of two things will
result: one, the optimizer won't realize its assumptions
have been violated, and bogus code will be generated; or
two, the optimizer _will_ realize its assumptions have been
violated, but will fall back to an ultra-conservative
position for code generation, preserving correctness but
sacrificing performance.

Neither of these possibilities is really good, but what's
worse is that they might start happening any time there is a
switch to a new compiler (or a different choice of compiler
options); worse still, unless you are very careful you will
start to get bad results before realizing what has happened.
There's an old Polish proverb that says, Even if you are the
person who buried all the land mines, walking through a
minefield is no fun.


(TIA: "an old Polish proverb" isn't really a Polish
proverb - it's meant to be humorous for the cognoscenti.)

jacob navia

6/15/2011 10:02:00 AM

0

Le 15/06/11 06:02, Michael Press a écrit :
> I want to index into a list using offset one indexing.
> The obvious thing to do is make a pointer that points
> one element before the list.
>
> void work_on_stuff(int *offset0, int length)
> {
> int offset1 = offset0 - 1;
>
> for(idx = 1; idx<= length; idx++)
> {
> ... offset1[idx] ...
> }
> }
>
> Is this likely to fail? Possible to fail?
> Very unlikely to fail? Not guaranteed by the standard?
>

Another evident way is to leave element zero unused and avoid
all problems... Why is that impossible?

Mark Bluemel

6/15/2011 10:08:00 AM

0

On 06/15/2011 05:02 AM, Michael Press wrote:
> I want to index into a list using offset one indexing.

Why?

gwowen

6/15/2011 10:55:00 AM

0

On Jun 15, 11:01 am, jacob navia <ja...@spamsink.net> wrote:

> Another evident way is to leave element zero unused and avoid
> all problems...

.... as long as you remember to make all your arrays one-element-too-
large, to avoid falling of the end.

Eric Sosman

6/15/2011 11:21:00 AM

0

On 6/15/2011 12:02 AM, Michael Press wrote:
> I want to index into a list using offset one indexing.
> The obvious thing to do is make a pointer that points
> one element before the list.
> [...]

This is Question 6.17 on the comp.lang.c Frequently Asked
Questions (FAQ) page at <http://www.c-fa....

--
Eric Sosman
esosman@ieee-dot-org.invalid

Shao Miller

6/15/2011 2:10:00 PM

0

On 6/15/2011 00:02, Michael Press wrote:
> I want to index into a list using offset one indexing.
> The obvious thing to do is make a pointer that points
> one element before the list.
>
> void work_on_stuff(int *offset0, int length)
> {
> int offset1 = offset0 - 1;
>
> for(idx = 1; idx<= length; idx++)
> {
> ... offset1[idx] ...
> }
> }
>
> Is this likely to fail? Possible to fail?
> Very unlikely to fail? Not guaranteed by the standard?
>

How about:

void work_on_stuff(int * array, int length) {
enum {start_pos = 1};
int idx;

for(idx = start_pos; idx <= length; ++idx) {
#define idx (idx - start_pos)
/* Work with array[idx] */
;
#undef idx
}
}

? Have a pleasant day.

Malcolm McLean

6/15/2011 2:31:00 PM

0

On Jun 15, 1:07 pm, Mark Bluemel <mark_blue...@pobox.com> wrote:
> On 06/15/2011 05:02 AM, Michael Press wrote:
>
> > I want to index into a list using offset one indexing.
>
> Why?
>
Many language use 1-based arrays. So do mathematicians.

If you've ever tried fiddling with code written half in Fortran and
half in C you'll realise that sometimes 1-based array indexing can be
tempting.

--
MiniBasic - how to write a script interpreter
http://www.lulu....

Joe Pfeiffer

6/15/2011 5:20:00 PM

0

Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> On Jun 15, 1:07 pm, Mark Bluemel <mark_blue...@pobox.com> wrote:
>> On 06/15/2011 05:02 AM, Michael Press wrote:
>>
>> > I want to index into a list using offset one indexing.
>>
>> Why?
>>
> Many language use 1-based arrays. So do mathematicians.
>
> If you've ever tried fiddling with code written half in Fortran and
> half in C you'll realise that sometimes 1-based array indexing can be
> tempting.

Giving new life to the old saying, "he can write FORTRAN in any
language".