array question - beginner question
Author Message
array question - beginner question

Hello,
Just a few quick questions, can someone help me with this wrap_around
function?

int wrap_around(int index, int size)
{
return(index-1 %size);

Quote:
}

why will this not work?

Also, if x is an array pointer, is x[-1] valid? Could this be shown as
*x [-1]?

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.

Tue, 29 Jan 2002 03:00:00 GMT
array question - beginner question
/ int wrap_around(int index, int size)
/ {
/ return(index-1 %size);
/ }

Do you mean
return ((index-1) % size)
?

/ Also, if x is an array pointer, is x[-1] valid? Could this be shown as

If x is an array
int x[42];
x[-1] is invalid.

If x is a pointer pointer (or pointer to anything else)
something *x;
x[-1] _might_ be valid. Depends on the value of x.

/ *x [-1]?

*x [-1] means either x[0][-1] or x[-1][0]. (I don't recall the precedence
of [] and *.)

--
Collective Against Consensual Sanity
http://www.angelfire.com/ca3/cacs
(umm...ignore that: there's only an empty index page for now)
For a free CACS T-shirt send \$42 shipping and handling.

Tue, 29 Jan 2002 03:00:00 GMT
array question - beginner question

Quote:
>Hello,
>Just a few quick questions, can someone help me with this wrap_around
>function?

>int wrap_around(int index, int size)
>{
>return(index-1 %size);
>}
>why will this not work?

Because for one thing, % has a higher precedence than -. So your expression
is parsed as

index - (1 % size)

What you want is

(index - 1) % size

Now, what if index is zero? Your program will{*filter*}up miserably. It will
present a negative operand to the % operator, which has an
implementation-defined result. It does not do the wrap around that you
expected.

Instead of subtracting 1, why don't you add the quantity (size - 1)?
This quantity is equal to -1, modulo size, but has a positive value:

return (index + size - 1) % size

Quote:
>Also, if x is an array pointer, is x[-1] valid? Could this be shown as

If x points to an array, rather than to an element of an array, then the proper
way to access the elements is like  (*x)[i] .  The x[i] form would be
used if x points to an array element.

Arrays start at element 0, so accessing element -1 leads to undefined behavior.
To index negatively, you must create a pointer that points somewhere in the
middle of the array, rather than at element zero.

Quote:
>*x [-1]?

This means *(x[-1]).

Tue, 29 Jan 2002 03:00:00 GMT
array question - beginner question

Quote:

> Hello,
> Just a few quick questions, can someone help me with this wrap_around
> function?

> int wrap_around(int index, int size)
> {
> return(index-1 %size);
> }
> why will this not work?

Multiplication operators have higher precedence than addition operators.
This should be

(index - 1) % size

to evaluate as you want.

Quote:

> Also, if x is an array pointer, is x[-1] valid? Could this be shown as
> *x [-1]?
> Thank you in advance! Hannah

> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.

Thu, 07 Feb 2002 03:00:00 GMT
array question - beginner question
Quote:
> Also, if x is an array pointer, is x[-1] valid? Could this be shown as
> *x [-1]?

Yes, unless x[0] just happens to be at the beginning of a bank of memory.
I use
this construct often, esplecially to "back up" after ++x.

As for the wrap around question, what Scott Kajihara wrote is certainly
true, but I thought
I'd share thefollowing I wrote for some junior engineers last year (forgive
the lack of context):
---------------------------------------------------------------------------

While optimizing interrupt code to eliminate missed interrupts, I
noticed
a construction used repeatedly that takes litterally an order of magnitude
more execution time than the construction I'm replacing it with, and so am
pointing this out with some hard statistics for your future referance.
What I've done is to first copy a piece of code typical of this
construction
then optimize it, then copy it with another piece of code and optimize it
in
several steps, each time timing 10 iterations of the code in in
deka-E-clocks.
The unoptimized code is:
static Word16  schedule[SCHEDULESLOTS];
static UWord16 current = 0;
current = (current + 1) SCHEDULESLOTS;
The optimized version, which runs 10 times faster, changes the last line
to:
if (current == SCHEDULESLOTS - 1) current = 0; else ++current;
The statistics for these 2 versions are:

526    current = (current + 1) SCHEDULESLOTS;
53    if (current == SCHEDULESLOTS - 1) current = 0; else ++current;

The second piece of code adds:
static Word16  chanIndex;
chanIndex = schedule[current];
The first optimization simply changes the definition of chanIndex:
int chanIndex;
The next is analogous to the optimization in the first example.
The third optimization replaces current with a pointer:
static Word16 * pCurrent = schedule;
NEXT_RING_ELEMENT(pCurrent, schedule, SCHEDULESLOTS) ;   chanIndex =
*pCurrent;
Where NEXT_RING_ELEMENT is a macro that I originally wrote for the flow
sensors' driver but have moved to utils.h:
#define   NEXT_RING_ELEMENT(p, ring, size)   { if (p == ring + size - 1) p
= ring; else ++p; }
The final versions declare first the pointer and then also chanIndex as
register variables:
register Word16 * pCurrent = schedule;
register Word16  chanIndex;
The statistics for these 6 versions are:

569    current = (current + 1) SCHEDULESLOTS;   chanIndex =
schedule[current];
623    current = (current + 1) SCHEDULESLOTS;   chanIndex =
schedule[current];
149    if (current == SCHEDULESLOTS - 1) current = 0; else ++current;
chanIndex = schedule[current];
145    NEXT_RING_ELEMENT(pCurrent, schedule, SCHEDULESLOTS) ;   chanIndex =
*pCurrent;
73    NEXT_RING_ELEMENT(pCurrent, schedule, SCHEDULESLOTS) ;   chanIndex =
*pCurrent;
47    NEXT_RING_ELEMENT(pCurrent, schedule, SCHEDULESLOTS) ;   chanIndex =
*pCurrent;
---------------------------------------------------------------------------

Quote:

> > Hello,
> > Just a few quick questions, can someone help me with this wrap_around
> > function?

> > int wrap_around(int index, int size)
> > {
> > return(index-1 %size);
> > }
> > why will this not work?

> Multiplication operators have higher precedence than addition operators.
> This should be

>   (index - 1) % size

> to evaluate as you want.

> > Thank you in advance! Hannah

> > Sent via Deja.com http://www.deja.com/
> > Share what you know. Learn what you don't.

Sat, 09 Feb 2002 03:00:00 GMT
array question - beginner question

Quote:
>   While optimizing interrupt code to eliminate missed interrupts, I
>noticed
>a construction used repeatedly that takes litterally an order of magnitude
>more execution time than the construction I'm replacing it with, and so am
>pointing this out with some hard statistics for your future referance.
>   What I've done is to first copy a piece of code typical of this
>construction
>then optimize it, then copy it with another piece of code and optimize it
>in
>several steps, each time timing 10 iterations of the code in in
>deka-E-clocks.
>   The unoptimized code is:
>static Word16  schedule[SCHEDULESLOTS];
>static UWord16 current = 0;
>current = (current + 1) SCHEDULESLOTS;

Surely you mean

current = (current + 1) % SCHEDULESLOTS;

The missing operator causes a syntax error.

Quote:
>   The optimized version, which runs 10 times faster, changes the last line
>to:
>if (current == SCHEDULESLOTS - 1) current = 0; else ++current;
>   The statistics for these 2 versions are:

You probably didn't make SCHEDULESLOTS a power-of-two number. Or your
compiler is too braindead to optimize the modulo operator into a
bitwise AND if the right operand is a power of two. A bitwise AND could
acutlaly be faster than selection statement, which will likely translate
into compare and branch instructions.

Also, why don't you just use ``unsigned int'' instead of this UWord16?  Does
the variable ``current'' have to conform to an external representation?

Quote:
>526    current = (current + 1) SCHEDULESLOTS;

Could your news posting software be removing % symbols?

Sat, 09 Feb 2002 03:00:00 GMT

 Page 1 of 1 [ 6 post ]

Relevant Pages