Memory Alignment question.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Greetings everyone,

I am using gcc 3.3.4 on a pentium M and noticed a
behaviour in memory allocation that confused me. So
far, the only thing I knew of memory
allocation/alignment was this: memory areas can only
be addressed by a multiple of the word size (in my
case a word is 4 bytes).

I did some testing to verify what I knew was correct
and was proved totally wrong. Here are two basic
examples

void function() {
char buffer1;
char buffer2;
}

Only 4 bytes are allocated for buffer1 & buffer2 (sub
$0x4 %esp) while I would have expected 8 bytes.
Someone told me it was for memory optimization which
seems rational but then... I guess one of the buffer
cannot be addressed by a multiple of 4 bytes, right?
Is not it a problem?

There is something even more confusing. I have been
chewing on it for a few days and came up with no
possible explanation. Take this:

void function() {
char buffer1[5];
char buffer2[6];
}

28 bytes are allocated for both buffers! I would have
expected 16 at most (8 for each). I did other tests
and came up with as surprising results.

Could anyone explain me how this works please and/or
point me to some documentation concerning this?

Thanks in advance,

          Barlad.


	

	
		
Créez gratuitement votre Yahoo! Mail avec 100 Mo de stockage !
Créez votre Yahoo! Mail sur http://fr.benefits.yahoo.com/

Yahoo! Messenger: dialoguez instantanément avec vos amis. Téléchargez GRATUITEMENT sur http://fr.messenger.yahoo.com

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux