Are arrays aligned in memory?

BlitzMax Forums/BlitzMax Programming/Are arrays aligned in memory?

ImaginaryHuman(Posted 2005) [#1]
I am just wondering, if you define an array of whatever type, e.g. integers or function pointers (yes I'm a function pointer maniac), will BlitzMax allocate the memory for the array aligned in memory to a multiple of the size of the type? (or of 4) ... so that when you access the elements, you read from memory addresses that are multiples of 4? Or does it just read from any memory address?


Bot Builder(Posted 2005) [#2]
Erm, I'm going to say most likely not. There is no point to aligning the array to the memory, that i know of. Arrays of ints will be increments of 4, longs incremnts of 8, shorts increments of 2, etc, but strings, arrays and objects are just increments of 4 because they are pointers.

Err, wait, do you mean an increment of the size of the type of thing it points to? No. Pointers are 4 bytes. One of the confusing things about saying blah:Byte Ptr.

Anyway, I'm not sure what you mean. Offsets to elements from the array's memory address will be multiples of the type's size, but not necissarily the actual address.


ImaginaryHuman(Posted 2005) [#3]
I mean, let's say, if I create an array, will it for example be stored at address 12 in memory (a multiple of 4) or will it be stored at 11, or 9, or 10?

The reason I ask is because I know that on most processors it is faster to read and write memory to aligned addresses. If I read an int from address 12, it might well be faster than reading from address 10 because the CPU might have to read the contents of 8 and 12 to produce a full integer value. Also writing an integer to 10 might require two writes, one to 8 and one to 12.

I recall this was an issue on the 68040 CPU whereby it was preferable to align memory accesses to multiples of 4. It proved to be a lot faster. Similar, some CPU's differ in that if you write 4 sequential bytes to memory, it may be just as fast as writing an integer, due to how the cache works.

What I'm trying to establish is whether I'm taking a performance penalty by using an array based on where in memory it is located.?


ImaginaryHuman(Posted 2005) [#4]
I did a few tests, and from what I can tell, it seems that all arrays are aligned to the nearest 8 bytes in memory. Arrays of bytes, shorts, int's, longs and doubles are all aligned to 8-byte boundaries. Which is good :-)


John Pickford(Posted 2005) [#5]
Phew!!! Disaster averted.


Warren(Posted 2005) [#6]
lol


skidracer(Posted 2005) [#7]
Once again, can you please stop posting non beginner related topics in the Beginner's Area.


ImaginaryHuman(Posted 2005) [#8]
Umm, this isn't the beginners area?


Hotcakes(Posted 2005) [#9]
It was before he moved it ;]

http://www.blitzbasic.com/Community/posts.php?topic=42550


ImaginaryHuman(Posted 2005) [#10]
Oh, I didn't spot that. Sorry I messed up again *doh*

I will cease creating topics in the beginners area altogether. I guess I'm not a beginner! :-) But I AM new to BlitzMax, hense the confusion and ambiguity. What defines something as `beginner` is subjective - new to BlitzMax? New to programming? Since I can't draw the line I'll just post any new topics/questions here then we can all share some cake :-)


Warren(Posted 2005) [#11]
I don't think something like "the aligmnent of array data in memory" classifies as a beginner topic. Most beginners are trying to figure out what the hell a function is.


ImaginaryHuman(Posted 2005) [#12]
Sure, if you're a beginner to programming in general, but what about if you are beginner to BlitzMax and don't know how to do something in it that you did in other languages? Are you not then a beginner?

Anyway, never mind, it's no biggie.


Hotcakes(Posted 2005) [#13]
Yeh I think the Beginner forum tends to lean more to those newbies to programming, rather than those newbies to BMax. I mean as far as BMax is concerned, we're pretty much all on equal ground here ('cept the beta testers:) so it doesn't really make much sense for -everyone- to post in it ;]