This is from memory, so I could be misusing a few words, but the meaning should be understandable
I'm currently at University, doing a BIT majoring in programming - We started C++, and when we started using arrays, our C++ teacher (a teacher with strange ideas and programming rules, such as no comments whatsoever allowed) told us that we should make our array sizes in multiples of 4 to be more efficient:
char exampleArrayChar[ 4 ]; //Allowed
float exampleArrayChar[ 6 ]; //NOT Allowed
int exampleArrayChar[ 8 ]; //Allowed
He said the reason behind this was because of the way the computer does its memory allocation.
The computer allocated the memory address / locations for each array element in groups of four bytes - so a 8 element array was done in 2 memory groups.
So the issue was that if you made an array of size 6, it would assign 2 groups of 4, but then mark 2 of those bytes (out of the 8) as invalid / void, rendering them unusable until the whole array was released from memory.
While this to me sounds plausible in regard to other computer math (such as 1gb = 1024mb instead of exactly 1000) I am interested in knowing:
- Just how true this is, and what the gain would be, if any, by doing this
- Is this just C++? (I would assume not, but still worth asking)
- Just some more info on this all round - for example, why 4? Isn't computer math normally binary, and thus 2's?
Looking around on the web I've been unable to find anything of major use or relevance.