Re:Linus on compiler warnings and code reviews
by doppelganger27 on Wednesday September 23, 2015 @03:45PM
(#10267626)
In C (and in all other languages I can think of), an array is actually a pointer to a bunch of memory. Indexing into the array calculates offset from the array pointer, and grabs the item at that offset.
[ Parent
| on HN ]
Re:Linus on compiler warnings and code reviews
by 36erhefg on Wednesday September 23, 2015 @04:40PM
(#10268008)
Array is not a pointer. Your description of indexing is not correct, neither is the phrase "array pointer" you invented. The machine code produced will calculate the offset differently for a pointer and an array.
Re:Linus on compiler warnings and code reviews
by doppelganger27 on Wednesday September 23, 2015 @04:51PM
(#10268098)
Let me clarify with an example. Suppose you have an array:
int a[10]
There is a section of memory of size
sizeof(int) * 10
somewhere, and the variable a is a
* int
that points to that section. When someone does this: int x = a[2]
It is equivalent to: int x = *(a+(sizeof(int)*2))
When I said "array pointer" in my previous comment, all it meant was the memory address that the variable (which is an array) points to (in the example, the variable "a")Edit: missing paren
Re:Linus on compiler warnings and code reviews
by 36erhefg on Wednesday September 23, 2015 @04:58PM
(#10268156)
a is a label, or an alias of a memory address. It is not a pointer. ( Your third example has a mistake, the correct increment is: a+2, because pointer arithmetic increments in object size not byte size )
Read this:http://eli.thegreenplace.net/2009/10/21/are-pointers-and-arr...
Re:Linus on compiler warnings and code reviews
by doppelganger27 on Wednesday September 23, 2015 @05:03PM
(#10268218)
Interesting read. Thanks :)
Re:Linus on compiler warnings and code reviews
by nickcw on Wednesday September 23, 2015 @03:52PM
(#10267679)
FYI the equivalent Go code would actually copy the whole array on the stack, and so it would be valid if possibly inefficient depending on the size of the array.