Skip to main content
GameDev.net gamedev.net
🔒 Locked

questions about array

Started by yuppies May 18, 2006 at 4:43 AM 2 replies 850+ views
Original Post
yuppies
yuppies
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 :)
Evil Steve
Evil Steve
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).
Nitage
Nitage
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.