Sign in to follow this  
yuppies

questions about array

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this