Re: arrays, pointers

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 02/18/04


Date: Wed, 18 Feb 2004 15:49:04 -0600

Kyle wrote:

>the point is, i read/heared somewhere, that ops on pointers are faster than
>those performed on arrays

Your original post was:

>int an_int, *array;
>array = new int[n];
>
> //would i want to do:
>an_int = array[i];
> //or rather:
>an_int = *(array+i);
> //and why

If "array" is a real array, or just a pointer as above, they mean the same
thing. In fact, the former is defined in terms of the latter. However, Per
Nilsson mentioned that this equivalence doesn't extend to types like
std::vector, which support only the subscripted syntax.

Above, the variable "array" is a pointer, which you've set to point at the
first element of a dynamically allocated array of int. Suppose it were a
real array, declared, say, like this:

 int array[N]; // N being a constant

Then these expressions are still equivalent:

1. an_int = array[i];
2. an_int = *(array+i);

First, the compiler transforms (1) into (2). Then, because "array" is an
actual array, it performs the standard array-to-pointer conversion, which
yields a pointer to its first element. You can think of this as the compiler
creating a temporary pointer variable, assigning it the address of the
array's first element, and using this temporary pointer in the arithmetic.

As for your efficiency concerns, there's no difference between subscripting
and pointer arithmetic, as the former is transformed at compile-time into
the latter. There are potential differences between using pointers vs.
arrays, but that's very much dependent on your usage pattern and compiler. A
compiler might recognize this idiom:

 while (*s++ = *t++) ...

but not the subscripted equivalent. As ever, I'd let clarity be my guide,
and if there's any doubt, profile.

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: Need help to port VAX code to Alpha and to Itaninum
    ... Not really, the original code was wrong, and the compiler was not ... pointer to the start of the array. ... So you are trying to pass a pointer to a pointer to an array where you ... Also start looking at where you can add the "const" modifier to function ...
    (comp.os.vms)
  • Re: Problem with log function - Intel fortran compiler under Linux
    ... Linux: Red Hat Enterprise Linux, ... I then tried with debugging on and array bound checking ... compiler is a rather common story. ... The program uses a single REAL array stored in a blank COMMON ...
    (comp.lang.fortran)
  • Re: decrement past beginning is valid?
    ... > What I meant by legal is that a compiler will compile it. ... > that an array is the same as a pointer. ... behave the same way on all platforms. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: gdb not catching out-of-bounds pointer
    ... is also not defined by the C-standard, IOW: writing code in anything ... provided the library writer knows what the compiler writer guarantees ... etc.) that don't point into the same array than I would about the sort ... of pointer aliasing issue that started this sub-thread. ...
    (comp.unix.programmer)
  • Re: null terminated strings
    ... pointer + 1 will point to the next element in the array. ... What I don't understand is why such a thing was included in any language that's more than an assembler. ... of structures that are not 'natural' the compiler need to generate ADD instructions using sizeof. ...
    (comp.os.vms)