Re: Differance between Array and Pointers




"DJ Delorie" <dj@xxxxxxxxxxx> wrote in message
news:xnslnqhp1a.fsf@xxxxxxxxxxxxxx

"Abstract Dissonance" <Abstract.Dissonance@xxxxxxxxxxx> writes:
Theres is no difference at the hardware level

Well, except that arrays tend to be much larger than pointers, and the
number of access cycles is different.

An array is a contiguous region of memory containing N elements of M
bytes each.

A pointer is a (usually) 2-8 byte value which contains the address of
some other data.


Umm. An array is a "virtual region of memory". Can you access more than one
element at a time? How do you access regions of memory at the lowest level?

Maybe you're confusing addresses with pointers?


Nope.

But guess what? the compiler converts all array access into pointer
access.

Er, no? When you pass an "array" to a function, it passes a pointer
to the array instead, but other than that the compiler does not
convert arrays into pointers. In fact, if you have a const array and
reference one element with an integer index, the compiler might
optimize the whole access away if it can.


Arrays are "virtual" objects and are not part of the hardware design(atleast
in every MP I've seen). If you are talking about an "array" you are just
talking about a region of memory that you have considered to be a group of
elements. How do you access those elements(at the lowest level)? Through a
"pointer".

For example, In Intel 80x86 there is no "array" opcode and "pointer"
opcode.

It does, however, have different addressing modes that are optimized
for arrays vs pointers.

Arrays: x = foo[j]
mov.l foo[4*esi],eax

Pointers: x = *foo;
mov.l [ebx],eax


Nope. Thats jut a pointer using scaled indexing.

I suppose that we have different concepts of pointers. You are treating
indexing as arrays while I see them just as pointers. I don't see a pointer
as necessarily a fixed address and don't consider offsets to that pointer an
array.

Could be my fault but I just don't see any technical difference. My there
is a very specific definition of a pointer?

would you consider

rep movs

as an array or a pointer reference?

The way I see it is that index and offset referencing are just making
pointers easier to work with. I guess you can call that method arrays if
you want to but I don't see any reason to make up a new word for something
that is suitiable in IMO.

If you apply your same logic to classes then you should be able to
demonstrate the ability to work with "efficiently" with classes at the low
level. It all ends up boiling down to "pointers" in the end no matter how
you look at it.

I suppose one could make a distinction to represent which "method" is more
efficient. (i.e., its generally better to use a rep then a loop or using
indexing vs. a loop). To me these are just means to make life easier when
working with pointers then constituiting a whole knew type of data. (i.e.,
arrays are just logical groups of bytes but no hardware I know of implements
a hardware data type called an array.).


I think we are arguing over semantics though. If the OP ment what you ment
then the obvious question is to go with what is most efficient(or, atleast
should be).

If someone is going to do something like

Loop1:
mov edi, [esi]
add esi, 4
loop Loop1

when they can just do

rep movsd

Then theres a serious problem(unless they intentionally chose the first one
for some good reason)

Both methods are the same, one uses pointers and the other "arrays"(Atleast
I think you will say that). To me they are exactly the same(i.e., they
accomplish the same task.. one is more efficient though).

Its similar to using a loop method

loop Some_Loop

or

dec ecx
jnz Some_Loop




They are exactly the same. Intel could have implemented loop as a macro and
just used the longer code. I definately wouldn't give the two "different"
methods a different name unless there was a good reason(and there might be).

I could be wrong though but I don't see what you have stated above as any
good reason making such a distinction. (i.e., a one that warrants any real
discussion over). If there is a good reason then, by all means, please
enlighten me.

As far as I'm concerned, Any time one has to do memory access then they are
working with pointers(directly or indirectly). In Masm/Tasm/Nasm that
consists of using any [ ] or any instruction that does that
"indirectly"(i.e., where there is an equivilent piece of code that uses
[ ]). You can call certain methods arrays and others pointers if you want
but I don't see any need. (because there is an equivilence and there is no
physical data element called an array in any hardware architecture I know
of(one has bytes, bits, nibbles, words, pages, etc...).

Although I guess one could take this equivilence idea to far and end up
doing everything with just one instruction and end up saying there is no use
for anything else.






.



Relevant Pages

  • Re: newbie question
    ... The second assigns a pointer to an array 1000 of char to an int pointer. ... What you pass are the file pointers, ... > second file is identical to the first. ... Look at your while loop. ...
    (comp.lang.c)
  • Re: loop performance question
    ... | such that every possible pair in the array is tested. ... | whenever I access any data in the second loop. ... The pointers to separately allocated objects break the ... Then comes the question of how to avoid cache misses when accessing obj2. ...
    (comp.lang.cpp)
  • Re: Dusty Deck and C memory manager, Part 1
    ... CMEMC.C -- C memory management routines for use with FORTRAN. ... Return an index into ARRAY representing LENGTH words of available ... void *ptr, *malloc; ... Rather than introduce Cray pointers and use C routines to allocate memory for Fortran arrays, you could use straight F90, for example: ...
    (comp.lang.fortran)
  • Re: Simple C containers, std::vector analog
    ... You're right about not needing to care very much about the memory ... blocks, in not knowing the ultimate size of the growable array, because ... there is no realloc function, and besides realloc might be having ... whether it's used for the list of array block pointers only or a C++ ...
    (comp.lang.c)
  • Re: Array of Pointers
    ... > Gerald wrote: ... >> I have a problem with an array of pointers. ... > complex) data structure than a simple array to do this efficiently. ... I'm aiming for accuracy, speed and low memory use, in that order. ...
    (comp.lang.c)