questions about array

Started by
2 comments, last by Nitage 17 years, 11 months ago
how is array represented in the memory or in stack? i got an argument from a japanese manager of mine saying that accessing of array using its index is verry slow since in actual representation(in ASM or in Memory/Stack) the array is traversed in memory before reaching the right index. Is this true? :) What are my options if these is true? what other data struct way is fast in accessing and storing value aside from "direct" access from an array. I just need some idea before i argue my boss :)
Advertisement
An array access is just a pointer addition and a dereference. It's slightly slower than accessing a variable normally, but only by a few clock cycles. Unless you're doing millions per frame, it's not going to be a problem. And if you access array index zero, it's just a pointer dereference.

It's laid out in memory as one contiguous block of memory, it doesn't get "traversed" for each access. Access is O(1).
I guess your manager is confusing arrays with linked-lists...
An array is represented in memory by a single continuous block of memory, the size of which is equal to the size of one array element * the number of elements in the array. Array access is achieved by taking the address of the array and adding an offset to it.

You boss seems to be thinking of a linked list, where to access an element you have to access every element in front of it in the list sequentially.

This topic is closed to new replies.

Advertisement